diff --git a/COPYING b/COPYING index eb5b75466..be5ebaedc 100644 --- a/COPYING +++ b/COPYING @@ -17,5 +17,5 @@ in this distribution is now licensed under the Apache License: Other Dependencies and Licenses: ================================ -Tesseract uses Leptonica library (http://leptonica.com/) with a very weakly -restricted copyright license (http://leptonica.com/about-the-license.html) +Tesseract uses Leptonica library (http://leptonica.com/) which essentially +uses a BSD 2-clause license. (http://leptonica.com/about-the-license.html) diff --git a/android/jni/Android.mk b/android/jni/Android.mk index 462480146..fd0e5112f 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -4,7 +4,7 @@ include $(CLEAR_VARS) LOCAL_MODULE := tesseract-$(APP_ABI) LOCAL_STATIC_LIBRARIES := \ - mobile_base \ + base \ leptonica-$(APP_ABI) LOCAL_C_INCLUDES := $(APP_C_INCLUDES) @@ -51,7 +51,6 @@ LOCAL_CFLAGS := -DANDROID_BUILD -DNO_CUBE_BUILD -DGRAPHICS_DISABLED include $(BUILD_SHARED_LIBRARY) -$(call import-module,mobile/base) -$(call import-module,mobile/base) +$(call import-module,base/port) $(call import-module,mobile/util/hash) $(call import-module,third_party/leptonica/android/jni) diff --git a/api/baseapi.cpp b/api/baseapi.cpp index a0689978e..b79e96917 100644 --- a/api/baseapi.cpp +++ b/api/baseapi.cpp @@ -809,9 +809,7 @@ int CubeAPITest(Boxa* boxa_blocks, Pixa* pixa_blocks, * has not been subjected to a call of Init, SetImage, Recognize, Clear, End * DetectOS, or anything else that changes the internal PAGE_RES. */ -PageIterator* TessBaseAPI::AnalyseLayout() { - return AnalyseLayout(false); -} +PageIterator* TessBaseAPI::AnalyseLayout() { return AnalyseLayout(false); } PageIterator* TessBaseAPI::AnalyseLayout(bool merge_similar_words) { if (FindLines() == 0) { @@ -1379,8 +1377,9 @@ static void AddBaselineCoordsTohOCR(const PageIterator *it, hocr_str->add_str_double(" ", round(p0 * 1000.0) / 1000.0); } -static void AddIdTohOCR(STRING* hocr_str, const std::string base, int num1, int num2) { - const unsigned long BUFSIZE = 64; +static void AddIdTohOCR(STRING* hocr_str, const std::string base, int num1, + int num2) { + const size_t BUFSIZE = 64; char id_buffer[BUFSIZE]; if (num2 >= 0) { snprintf(id_buffer, BUFSIZE - 1, "%s_%d_%d", base.c_str(), num1, num2); @@ -1393,8 +1392,7 @@ static void AddIdTohOCR(STRING* hocr_str, const std::string base, int num1, int *hocr_str += "'"; } -static void AddBoxTohOCR(const ResultIterator *it, - PageIteratorLevel level, +static void AddBoxTohOCR(const ResultIterator* it, PageIteratorLevel level, STRING* hocr_str) { int left, top, right, bottom; it->BoundingBox(level, &left, &top, &right, &bottom); @@ -1410,7 +1408,7 @@ static void AddBoxTohOCR(const ResultIterator *it, // add custom height measures float row_height, descenders, ascenders; // row attributes it->RowAttributes(&row_height, &descenders, &ascenders); - // TODO: Do we want to limit these to a single decimal place? + // TODO(rays): Do we want to limit these to a single decimal place? hocr_str->add_str_double("; x_size ", row_height); hocr_str->add_str_double("; x_descenders ", descenders * -1); hocr_str->add_str_double("; x_ascenders ", ascenders); @@ -1418,9 +1416,8 @@ static void AddBoxTohOCR(const ResultIterator *it, *hocr_str += "\">"; } -static void AddBoxToTSV(const PageIterator *it, - PageIteratorLevel level, - STRING* hocr_str) { +static void AddBoxToTSV(const PageIterator* it, PageIteratorLevel level, + STRING* hocr_str) { int left, top, right, bottom; it->BoundingBox(level, &left, &top, &right, &bottom); hocr_str->add_str_int("\t", left); @@ -1429,8 +1426,6 @@ static void AddBoxToTSV(const PageIterator *it, hocr_str->add_str_int("\t", bottom - top); } - - /** * Make a HTML-formatted string with hOCR markup from the internal * data structures. @@ -1440,7 +1435,7 @@ static void AddBoxToTSV(const PageIterator *it, * STL removed from original patch submission and refactored by rays. */ char* TessBaseAPI::GetHOCRText(int page_number) { - return GetHOCRText(NULL,page_number); + return GetHOCRText(NULL, page_number); } /** @@ -1452,13 +1447,12 @@ char* TessBaseAPI::GetHOCRText(int page_number) { * STL removed from original patch submission and refactored by rays. */ char* TessBaseAPI::GetHOCRText(ETEXT_DESC* monitor, int page_number) { - if (tesseract_ == NULL || - (page_res_ == NULL && Recognize(monitor) < 0)) + if (tesseract_ == NULL || (page_res_ == NULL && Recognize(monitor) < 0)) return NULL; int lcnt = 1, bcnt = 1, pcnt = 1, wcnt = 1; int page_id = page_number + 1; // hOCR uses 1-based page numbers. - bool para_is_ltr = true; // Default direction is LTR + bool para_is_ltr = true; // Default direction is LTR const char* paragraph_lang = NULL; bool font_info = false; GetBoolVariable("hocr_font_info", &font_info); @@ -1470,13 +1464,13 @@ char* TessBaseAPI::GetHOCRText(ETEXT_DESC* monitor, int page_number) { #ifdef _WIN32 // convert input name from ANSI encoding to utf-8 - int str16_len = MultiByteToWideChar(CP_ACP, 0, input_file_->string(), -1, - NULL, 0); + int str16_len = + MultiByteToWideChar(CP_ACP, 0, input_file_->string(), -1, NULL, 0); wchar_t *uni16_str = new WCHAR[str16_len]; str16_len = MultiByteToWideChar(CP_ACP, 0, input_file_->string(), -1, uni16_str, str16_len); - int utf8_len = WideCharToMultiByte(CP_UTF8, 0, uni16_str, str16_len, NULL, - 0, NULL, NULL); + int utf8_len = WideCharToMultiByte(CP_UTF8, 0, uni16_str, str16_len, NULL, 0, + NULL, NULL); char *utf8_str = new char[utf8_len]; WideCharToMultiByte(CP_UTF8, 0, uni16_str, str16_len, utf8_str, utf8_len, NULL, NULL); @@ -1509,7 +1503,7 @@ char* TessBaseAPI::GetHOCRText(ETEXT_DESC* monitor, int page_number) { // Open any new block/paragraph/textline. if (res_it->IsAtBeginningOf(RIL_BLOCK)) { - para_is_ltr = true; // reset to default direction + para_is_ltr = true; // reset to default direction hocr_str += "
WordRecognitionLanguage(); if (paragraph_lang) { - hocr_str += " lang='"; - hocr_str += paragraph_lang; - hocr_str += "'"; + hocr_str += " lang='"; + hocr_str += paragraph_lang; + hocr_str += "'"; } AddBoxTohOCR(res_it, RIL_PARA, &hocr_str); } @@ -1567,8 +1561,12 @@ char* TessBaseAPI::GetHOCRText(ETEXT_DESC* monitor, int page_number) { } switch (res_it->WordDirection()) { // Only emit direction if different from current paragraph direction - case DIR_LEFT_TO_RIGHT: if (!para_is_ltr) hocr_str += " dir='ltr'"; break; - case DIR_RIGHT_TO_LEFT: if (para_is_ltr) hocr_str += " dir='rtl'"; break; + case DIR_LEFT_TO_RIGHT: + if (!para_is_ltr) hocr_str += " dir='ltr'"; + break; + case DIR_RIGHT_TO_LEFT: + if (para_is_ltr) hocr_str += " dir='rtl'"; + break; case DIR_MIX: case DIR_NEUTRAL: default: // Do nothing. @@ -1600,7 +1598,7 @@ char* TessBaseAPI::GetHOCRText(ETEXT_DESC* monitor, int page_number) { if (last_word_in_para) { hocr_str += "\n

\n"; pcnt++; - para_is_ltr = true; // back to default direction + para_is_ltr = true; // back to default direction } if (last_word_in_block) { hocr_str += "
\n"; @@ -1620,8 +1618,7 @@ char* TessBaseAPI::GetHOCRText(ETEXT_DESC* monitor, int page_number) { * page_number is 0-based but will appear in the output as 1-based. */ char* TessBaseAPI::GetTSVText(int page_number) { - if (tesseract_ == NULL || - (page_res_ == NULL && Recognize(NULL) < 0)) + if (tesseract_ == NULL || (page_res_ == NULL && Recognize(NULL) < 0)) return NULL; int lcnt = 1, bcnt = 1, pcnt = 1, wcnt = 1; @@ -1629,9 +1626,10 @@ char* TessBaseAPI::GetTSVText(int page_number) { STRING tsv_str(""); - int page_num = page_id, block_num = 0, par_num = 0, line_num = 0, word_num = 0; + int page_num = page_id, block_num = 0, par_num = 0, line_num = 0, + word_num = 0; - tsv_str.add_str_int("1\t", page_num); // level 1 - page + tsv_str.add_str_int("1\t", page_num); // level 1 - page tsv_str.add_str_int("\t", block_num); tsv_str.add_str_int("\t", par_num); tsv_str.add_str_int("\t", line_num); @@ -1642,7 +1640,7 @@ char* TessBaseAPI::GetTSVText(int page_number) { tsv_str.add_str_int("\t", rect_height_); tsv_str += "\t-1\t\n"; - ResultIterator *res_it = GetIterator(); + ResultIterator* res_it = GetIterator(); while (!res_it->Empty(RIL_BLOCK)) { if (res_it->Empty(RIL_WORD)) { res_it->Next(RIL_WORD); @@ -1652,46 +1650,46 @@ char* TessBaseAPI::GetTSVText(int page_number) { // Add rows for any new block/paragraph/textline. if (res_it->IsAtBeginningOf(RIL_BLOCK)) { block_num++, par_num = 0, line_num = 0, word_num = 0; - tsv_str.add_str_int("2\t", page_num); // level 2 - block + tsv_str.add_str_int("2\t", page_num); // level 2 - block tsv_str.add_str_int("\t", block_num); tsv_str.add_str_int("\t", par_num); tsv_str.add_str_int("\t", line_num); tsv_str.add_str_int("\t", word_num); AddBoxToTSV(res_it, RIL_BLOCK, &tsv_str); - tsv_str += "\t-1\t\n"; // end of row for block + tsv_str += "\t-1\t\n"; // end of row for block } if (res_it->IsAtBeginningOf(RIL_PARA)) { par_num++, line_num = 0, word_num = 0; - tsv_str.add_str_int("3\t", page_num); // level 3 - paragraph + tsv_str.add_str_int("3\t", page_num); // level 3 - paragraph tsv_str.add_str_int("\t", block_num); tsv_str.add_str_int("\t", par_num); tsv_str.add_str_int("\t", line_num); tsv_str.add_str_int("\t", word_num); AddBoxToTSV(res_it, RIL_PARA, &tsv_str); - tsv_str += "\t-1\t\n"; // end of row for para + tsv_str += "\t-1\t\n"; // end of row for para } if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) { line_num++, word_num = 0; - tsv_str.add_str_int("4\t", page_num); // level 4 - line + tsv_str.add_str_int("4\t", page_num); // level 4 - line tsv_str.add_str_int("\t", block_num); tsv_str.add_str_int("\t", par_num); tsv_str.add_str_int("\t", line_num); tsv_str.add_str_int("\t", word_num); AddBoxToTSV(res_it, RIL_TEXTLINE, &tsv_str); - tsv_str += "\t-1\t\n"; // end of row for line + tsv_str += "\t-1\t\n"; // end of row for line } // Now, process the word... int left, top, right, bottom; bool bold, italic, underlined, monospace, serif, smallcaps; int pointsize, font_id; - const char *font_name; + const char* font_name; res_it->BoundingBox(RIL_WORD, &left, &top, &right, &bottom); - font_name = res_it->WordFontAttributes(&bold, &italic, &underlined, - &monospace, &serif, &smallcaps, - &pointsize, &font_id); + font_name = + res_it->WordFontAttributes(&bold, &italic, &underlined, &monospace, + &serif, &smallcaps, &pointsize, &font_id); word_num++; - tsv_str.add_str_int("5\t", page_num); // level 5 - word + tsv_str.add_str_int("5\t", page_num); // level 5 - word tsv_str.add_str_int("\t", block_num); tsv_str.add_str_int("\t", par_num); tsv_str.add_str_int("\t", line_num); @@ -1712,11 +1710,11 @@ char* TessBaseAPI::GetTSVText(int page_number) { tsv_str += res_it->GetUTF8Text(RIL_SYMBOL); res_it->Next(RIL_SYMBOL); } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD)); - tsv_str += "\n"; // end of row + tsv_str += "\n"; // end of row wcnt++; } - char *ret = new char[tsv_str.length() + 1]; + char* ret = new char[tsv_str.length() + 1]; strcpy(ret, tsv_str.string()); delete res_it; return ret; @@ -1760,7 +1758,7 @@ char* TessBaseAPI::GetBoxText(int page_number) { int total_length = blob_count * kBytesPerBoxFileLine + utf8_length + kMaxBytesPerLine; char* result = new char[total_length]; - strcpy(result, "\0"); + result[0] = '\0'; int output_length = 0; LTRResultIterator* it = GetLTRIterator(); do { @@ -1907,17 +1905,17 @@ char* TessBaseAPI::GetUNLVText() { return result; } - /** - * The recognized text is returned as a char* which is coded - * as UTF8 and must be freed with the delete [] operator. - * page_number is a 0-based page index that will appear in the osd file. - */ +/** + * The recognized text is returned as a char* which is coded + * as UTF8 and must be freed with the delete [] operator. + * page_number is a 0-based page index that will appear in the osd file. + */ char* TessBaseAPI::GetOsdText(int page_number) { OSResults osr; bool osd = DetectOS(&osr); if (!osd) { - return NULL; + return NULL; } int orient_id = osr.best_result.orientation_id; @@ -1931,19 +1929,19 @@ char* TessBaseAPI::GetOsdText(int page_number) { int orient_deg = orient_id * 90; // clockwise rotation needed to make the page upright - int rotate = OrientationIdToValue(orient_id); + int rotate = OrientationIdToValue(orient_id); - char* osd_buf = new char[255]; - snprintf(osd_buf, 255, - "Page number: %d\n" - "Orientation in degrees: %d\n" - "Rotate: %d\n" - "Orientation confidence: %.2f\n" - "Script: %s\n" - "Script confidence: %.2f\n", - page_number, - orient_deg, rotate, orient_conf, - script_name, script_conf); + const int kOsdBufsize = 255; + char* osd_buf = new char[kOsdBufsize]; + snprintf(osd_buf, kOsdBufsize, + "Page number: %d\n" + "Orientation in degrees: %d\n" + "Rotate: %d\n" + "Orientation confidence: %.2f\n" + "Script: %s\n" + "Script confidence: %.2f\n", + page_number, orient_deg, rotate, orient_conf, script_name, + script_conf); return osd_buf; } @@ -2063,7 +2061,7 @@ void TessBaseAPI::Clear() { if (thresholder_ != NULL) thresholder_->Clear(); ClearResults(); - SetInputImage(NULL); + if (tesseract_ != NULL) SetInputImage(NULL); } /** @@ -2767,7 +2765,7 @@ void TessBaseAPI::GetFeaturesForBlob(TBLOB* blob, INT_FX_RESULT_STRUCT fx_info; tesseract_->ExtractFeatures(*blob, false, &bl_features, &cn_features, &fx_info, &outline_counts); - if (cn_features.size() == 0 || cn_features.size() > MAX_NUM_INT_FEATURES) { + if (cn_features.empty() || cn_features.size() > MAX_NUM_INT_FEATURES) { *num_features = 0; return; // Feature extraction failed. } diff --git a/api/baseapi.h b/api/baseapi.h index 3b0d3f67c..d872689ee 100644 --- a/api/baseapi.h +++ b/api/baseapi.h @@ -588,8 +588,8 @@ class TESS_API TessBaseAPI { * data structures. * page_number is 0-based but will appear in the output as 1-based. * monitor can be used to - * cancel the recognition - * receive progress callbacks + * cancel the recognition + * receive progress callbacks */ char* GetHOCRText(ETEXT_DESC* monitor, int page_number); @@ -750,13 +750,9 @@ class TESS_API TessBaseAPI { */ static void NormalizeTBLOB(TBLOB *tblob, ROW *row, bool numeric_mode); - Tesseract* tesseract() const { - return tesseract_; - } + Tesseract* tesseract() const { return tesseract_; } - OcrEngineMode oem() const { - return last_oem_requested_; - } + OcrEngineMode oem() const { return last_oem_requested_; } void InitTruthCallback(TruthCallback *cb) { truth_cb_ = cb; } @@ -898,7 +894,7 @@ class TESS_API TessBaseAPI { const char* retry_config, int timeout_millisec, TessResultRenderer* renderer, int tessedit_page_number); - // TIFF supports multipage so gets special consideration + // TIFF supports multipage so gets special consideration. bool ProcessPagesMultipageTiff(const unsigned char *data, size_t size, const char* filename, diff --git a/api/pdfrenderer.cpp b/api/pdfrenderer.cpp index 470830049..10c6564ff 100644 --- a/api/pdfrenderer.cpp +++ b/api/pdfrenderer.cpp @@ -20,12 +20,12 @@ #include "config_auto.h" #endif +#include "allheaders.h" #include "baseapi.h" -#include "renderer.h" #include "math.h" +#include "renderer.h" #include "strngs.h" #include "tprintf.h" -#include "allheaders.h" #ifdef _MSC_VER #include "mathfix.h" @@ -441,8 +441,8 @@ char* TessPDFRenderer::GetPDFTextObjects(TessBaseAPI* api, int code = unicodes[i]; // Convert to UTF-16BE https://en.wikipedia.org/wiki/UTF-16 if ((code > 0xD7FF && code < 0xE000) || code > 0x10FFFF) { - tprintf("Dropping invalid codepoint %d\n", code); - continue; + tprintf("Dropping invalid codepoint %d\n", code); + continue; } if (code < 0x10000) { snprintf(utf16, sizeof(utf16), "<%04X>", code); @@ -567,7 +567,8 @@ bool TessPDFRenderer::BeginDocumentHandler() { "<<\n" " /Length %lu /Filter /FlateDecode\n" ">>\n" - "stream\n", (unsigned long)len); + "stream\n", + (unsigned long)len); if (n >= sizeof(buf)) { lept_free(comp); return false; diff --git a/api/renderer.cpp b/api/renderer.cpp index 4a88a2460..e68314938 100644 --- a/api/renderer.cpp +++ b/api/renderer.cpp @@ -155,11 +155,11 @@ TessHOcrRenderer::TessHOcrRenderer(const char *outputbase, bool font_info) bool TessHOcrRenderer::BeginDocumentHandler() { AppendString( - "\n" - "\n" - "\n \n "); + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" + " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" + "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" " + "lang=\"en\">\n <head>\n <title>"); AppendString(title()); AppendString( "\n" @@ -198,25 +198,25 @@ bool TessHOcrRenderer::AddImageHandler(TessBaseAPI* api) { /********************************************************************** * TSV Text Renderer interface implementation **********************************************************************/ -TessTsvRenderer::TessTsvRenderer(const char *outputbase) +TessTsvRenderer::TessTsvRenderer(const char* outputbase) : TessResultRenderer(outputbase, "tsv") { - font_info_ = false; + font_info_ = false; } -TessTsvRenderer::TessTsvRenderer(const char *outputbase, bool font_info) +TessTsvRenderer::TessTsvRenderer(const char* outputbase, bool font_info) : TessResultRenderer(outputbase, "tsv") { - font_info_ = font_info; + font_info_ = font_info; } bool TessTsvRenderer::BeginDocumentHandler() { // Output TSV column headings - AppendString("level\tpage_num\tblock_num\tpar_num\tline_num\tword_num\tleft\ttop\twidth\theight\tconf\ttext\n"); + AppendString( + "level\tpage_num\tblock_num\tpar_num\tline_num\tword_" + "num\tleft\ttop\twidth\theight\tconf\ttext\n"); return true; } -bool TessTsvRenderer::EndDocumentHandler() { - return true; -} +bool TessTsvRenderer::EndDocumentHandler() { return true; } bool TessTsvRenderer::AddImageHandler(TessBaseAPI* api) { char* tsv = api->GetTSVText(imagenum()); @@ -266,8 +266,7 @@ bool TessBoxTextRenderer::AddImageHandler(TessBaseAPI* api) { * Osd Text Renderer interface implementation **********************************************************************/ TessOsdRenderer::TessOsdRenderer(const char* outputbase) - : TessResultRenderer(outputbase, "osd") { -} + : TessResultRenderer(outputbase, "osd") {} bool TessOsdRenderer::AddImageHandler(TessBaseAPI* api) { char* osd = api->GetOsdText(imagenum()); diff --git a/api/renderer.h b/api/renderer.h index 6b47813f7..ad9e4d03b 100644 --- a/api/renderer.h +++ b/api/renderer.h @@ -153,13 +153,13 @@ class TESS_API TessHOcrRenderer : public TessResultRenderer { explicit TessHOcrRenderer(const char *outputbase, bool font_info); explicit TessHOcrRenderer(const char *outputbase); -protected: + protected: virtual bool BeginDocumentHandler(); virtual bool AddImageHandler(TessBaseAPI* api); virtual bool EndDocumentHandler(); -private: - bool font_info_; // whether to print font information + private: + bool font_info_; // whether to print font information }; /** @@ -167,15 +167,15 @@ private: */ class TESS_API TessTsvRenderer : public TessResultRenderer { public: - explicit TessTsvRenderer(const char *outputbase, bool font_info); - explicit TessTsvRenderer(const char *outputbase); + explicit TessTsvRenderer(const char* outputbase, bool font_info); + explicit TessTsvRenderer(const char* outputbase); -protected: + protected: virtual bool BeginDocumentHandler(); virtual bool AddImageHandler(TessBaseAPI* api); virtual bool EndDocumentHandler(); -private: + private: bool font_info_; // whether to print font information }; @@ -188,12 +188,12 @@ class TESS_API TessPDFRenderer : public TessResultRenderer { // we load a custom PDF font from this location. TessPDFRenderer(const char *outputbase, const char *datadir); -protected: + protected: virtual bool BeginDocumentHandler(); virtual bool AddImageHandler(TessBaseAPI* api); virtual bool EndDocumentHandler(); -private: + private: // We don't want to have every image in memory at once, // so we store some metadata as we go along producing // PDFs one page at a time. At the end that metadata is diff --git a/api/tesseractmain.cpp b/api/tesseractmain.cpp index 3fe8dc8c5..fe07e2af8 100644 --- a/api/tesseractmain.cpp +++ b/api/tesseractmain.cpp @@ -40,28 +40,28 @@ static void Win32WarningHandler(const char* module, const char* fmt, va_list ap) { - if (module != NULL) { - fprintf(stderr, "%s: ", module); - } - fprintf(stderr, "Warning, "); - vfprintf(stderr, fmt, ap); - fprintf(stderr, ".\n"); + if (module != NULL) { + fprintf(stderr, "%s: ", module); + } + fprintf(stderr, "Warning, "); + vfprintf(stderr, fmt, ap); + fprintf(stderr, ".\n"); } #endif /* HAVE_TIFFIO_H && _WIN32 */ void PrintVersionInfo() { - char *versionStrP; + char* versionStrP; - printf("tesseract %s\n", tesseract::TessBaseAPI::Version()); + printf("tesseract %s\n", tesseract::TessBaseAPI::Version()); - versionStrP = getLeptonicaVersion(); - printf(" %s\n", versionStrP); - lept_free(versionStrP); + versionStrP = getLeptonicaVersion(); + printf(" %s\n", versionStrP); + lept_free(versionStrP); - versionStrP = getImagelibVersions(); - printf(" %s\n", versionStrP); - lept_free(versionStrP); + versionStrP = getImagelibVersions(); + printf(" %s\n", versionStrP); + lept_free(versionStrP); #ifdef USE_OPENCL cl_platform_id platform; @@ -82,7 +82,7 @@ void PrintVersionInfo() { printf(" Found %d devices.\n", num_devices); for (i = 0; i < num_devices; ++i) { clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 256, info, 0); - printf(" Device %d name: %s.\n", i+1, info); + printf(" Device %d name: %s.\n", i + 1, info); } #endif } @@ -100,27 +100,22 @@ void PrintUsage(const char* program) { void PrintHelpForPSM() { const char* msg = "Page segmentation modes:\n" - " 0 Orientation and script detection (OSD) only.\n" - " 1 Automatic page segmentation with OSD.\n" - " 2 Automatic page segmentation, but no OSD, or OCR.\n" - " 3 Fully automatic page segmentation, but no OSD. (Default)\n" - " 4 Assume a single column of text of variable sizes.\n" - " 5 Assume a single uniform block of vertically aligned text.\n" - " 6 Assume a single uniform block of text.\n" - " 7 Treat the image as a single text line.\n" - " 8 Treat the image as a single word.\n" - " 9 Treat the image as a single word in a circle.\n" - " 10 Treat the image as a single character.\n" - - //TODO: Consider publishing these modes. - #if 0 - " 11 Sparse text. Find as much text as possible in no" - " particular order.\n" - " 12 Sparse text with OSD.\n" - " 13 Raw line. Treat the image as a single text line,\n" - "\t\t\tbypassing hacks that are Tesseract-specific.\n" - #endif - ; + " 0 Orientation and script detection (OSD) only.\n" + " 1 Automatic page segmentation with OSD.\n" + " 2 Automatic page segmentation, but no OSD, or OCR.\n" + " 3 Fully automatic page segmentation, but no OSD. (Default)\n" + " 4 Assume a single column of text of variable sizes.\n" + " 5 Assume a single uniform block of vertically aligned text.\n" + " 6 Assume a single uniform block of text.\n" + " 7 Treat the image as a single text line.\n" + " 8 Treat the image as a single word.\n" + " 9 Treat the image as a single word in a circle.\n" + " 10 Treat the image as a single character.\n" + " 11 Sparse text. Find as much text as possible in no" + " particular order.\n" + " 12 Sparse text with OSD.\n" + " 13 Raw line. Treat the image as a single text line,\n" + "\t\t\tbypassing hacks that are Tesseract-specific.\n"; printf("%s", msg); } @@ -137,31 +132,30 @@ void PrintHelpMessage(const char* program) { " -c VAR=VALUE Set value for config variables.\n" " Multiple -c arguments are allowed.\n" " -psm NUM Specify page segmentation mode.\n" - "NOTE: These options must occur before any configfile.\n" - ; + "NOTE: These options must occur before any configfile.\n"; printf("\n%s\n", ocr_options); PrintHelpForPSM(); - const char *single_options = + const char* single_options = "Single options:\n" " -h, --help Show this help message.\n" " --help-psm Show page segmentation modes.\n" " -v, --version Show version information.\n" " --list-langs List available languages for tesseract engine.\n" - " --print-parameters Print tesseract parameters to stdout.\n" - ; + " --print-parameters Print tesseract parameters to stdout.\n"; printf("\n%s", single_options); } -void SetVariablesFromCLArgs(tesseract::TessBaseAPI* api, int argc, char** argv) { +void SetVariablesFromCLArgs(tesseract::TessBaseAPI* api, int argc, + char** argv) { char opt1[256], opt2[255]; for (int i = 0; i < argc; i++) { if (strcmp(argv[i], "-c") == 0 && i + 1 < argc) { strncpy(opt1, argv[i + 1], 255); opt1[255] = '\0'; - char *p = strchr(opt1, '='); + char* p = strchr(opt1, '='); if (!p) { fprintf(stderr, "Missing = in configvar assignment\n"); exit(1); @@ -190,8 +184,8 @@ void PrintLangsList(tesseract::TessBaseAPI* api) { } void PrintBanner() { - tprintf("Tesseract Open Source OCR Engine v%s with Leptonica\n", - tesseract::TessBaseAPI::Version()); + tprintf("Tesseract Open Source OCR Engine v%s with Leptonica\n", + tesseract::TessBaseAPI::Version()); } /** @@ -209,31 +203,25 @@ void PrintBanner() { * but that doesn't work. */ void FixPageSegMode(tesseract::TessBaseAPI* api, - tesseract::PageSegMode pagesegmode) { + tesseract::PageSegMode pagesegmode) { if (api->GetPageSegMode() == tesseract::PSM_SINGLE_BLOCK) - api->SetPageSegMode(pagesegmode); + api->SetPageSegMode(pagesegmode); } // NOTE: arg_i is used here to avoid ugly *i so many times in this function -void ParseArgs(const int argc, char** argv, - const char** lang, - const char** image, - const char** outputbase, - const char** datapath, - bool* list_langs, - bool* print_parameters, - GenericVector* vars_vec, - GenericVector* vars_values, - int* arg_i, - tesseract::PageSegMode* pagesegmode) { +void ParseArgs(const int argc, char** argv, const char** lang, + const char** image, const char** outputbase, + const char** datapath, bool* list_langs, bool* print_parameters, + GenericVector* vars_vec, + GenericVector* vars_values, int* arg_i, + tesseract::PageSegMode* pagesegmode) { if (argc == 1) { PrintHelpMessage(argv[0]); exit(0); } if (argc == 2) { - if ((strcmp(argv[1], "-h") == 0) || - (strcmp(argv[1], "--help") == 0)) { + if ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0)) { PrintHelpMessage(argv[0]); exit(0); } @@ -241,8 +229,7 @@ void ParseArgs(const int argc, char** argv, PrintHelpForPSM(); exit(0); } - if ((strcmp(argv[1], "-v") == 0) || - (strcmp(argv[1], "--version") == 0)) { + if ((strcmp(argv[1], "-v") == 0) || (strcmp(argv[1], "--version") == 0)) { PrintVersionInfo(); exit(0); } @@ -298,10 +285,10 @@ void ParseArgs(const int argc, char** argv, } } -void PreloadRenderers(tesseract::TessBaseAPI* api, - tesseract::PointerVector* renderers, - tesseract::PageSegMode pagesegmode, - const char* outputbase) { +void PreloadRenderers( + tesseract::TessBaseAPI* api, + tesseract::PointerVector* renderers, + tesseract::PageSegMode pagesegmode, const char* outputbase) { if (pagesegmode == tesseract::PSM_OSD_ONLY) { renderers->push_back(new tesseract::TessOsdRenderer(outputbase)); } else { @@ -311,7 +298,7 @@ void PreloadRenderers(tesseract::TessBaseAPI* api, bool font_info; api->GetBoolVariable("hocr_font_info", &font_info); renderers->push_back( - new tesseract::TessHOcrRenderer(outputbase, font_info)); + new tesseract::TessHOcrRenderer(outputbase, font_info)); } api->GetBoolVariable("tessedit_create_tsv", &b); @@ -324,8 +311,8 @@ void PreloadRenderers(tesseract::TessBaseAPI* api, api->GetBoolVariable("tessedit_create_pdf", &b); if (b) { - renderers->push_back(new tesseract::TessPDFRenderer(outputbase, - api->GetDatapath())); + renderers->push_back( + new tesseract::TessPDFRenderer(outputbase, api->GetDatapath())); } api->GetBoolVariable("tessedit_write_unlv", &b); @@ -359,8 +346,7 @@ void PreloadRenderers(tesseract::TessBaseAPI* api, * **********************************************************************/ - -int main(int argc, char **argv) { +int main(int argc, char** argv) { const char* lang = "eng"; const char* image = NULL; const char* outputbase = NULL; @@ -380,10 +366,8 @@ int main(int argc, char **argv) { TIFFSetWarningHandler(Win32WarningHandler); #endif /* HAVE_TIFFIO_H && _WIN32 */ - ParseArgs(argc, argv, - &lang, &image, &outputbase, &datapath, - &list_langs, &print_parameters, - &vars_vec, &vars_values, &arg_i, &pagesegmode); + ParseArgs(argc, argv, &lang, &image, &outputbase, &datapath, &list_langs, + &print_parameters, &vars_vec, &vars_values, &arg_i, &pagesegmode); bool banner = false; if (outputbase != NULL && strcmp(outputbase, "-") && @@ -396,8 +380,9 @@ int main(int argc, char **argv) { api.SetOutputName(outputbase); - int init_failed = api.Init(datapath, lang, tesseract::OEM_DEFAULT, - &(argv[arg_i]), argc - arg_i, &vars_vec, &vars_values, false); + int init_failed = + api.Init(datapath, lang, tesseract::OEM_DEFAULT, &(argv[arg_i]), + argc - arg_i, &vars_vec, &vars_values, false); if (init_failed) { fprintf(stderr, "Could not initialize tesseract.\n"); exit(1); @@ -406,8 +391,8 @@ int main(int argc, char **argv) { SetVariablesFromCLArgs(&api, argc, argv); if (list_langs) { - PrintLangsList(&api); - exit(0); + PrintLangsList(&api); + exit(0); } if (print_parameters) { @@ -436,12 +421,13 @@ int main(int argc, char **argv) { tesseract::TextlineOrder order; float deskew_angle; - tesseract::PageIterator* it = api.AnalyseLayout(); + tesseract::PageIterator* it = api.AnalyseLayout(); if (it) { it->Orientation(&orientation, &direction, &order, &deskew_angle); - tprintf("Orientation: %d\nWritingDirection: %d\nTextlineOrder: %d\n" \ - "Deskew angle: %.4f\n", - orientation, direction, order, deskew_angle); + tprintf( + "Orientation: %d\nWritingDirection: %d\nTextlineOrder: %d\n" + "Deskew angle: %.4f\n", + orientation, direction, order, deskew_angle); } else { ret_val = 1; } @@ -456,14 +442,12 @@ int main(int argc, char **argv) { // ambigs.train, box.train, box.train.stderr, linebox, rebox bool b = false; bool in_training_mode = - (api.GetBoolVariable("tessedit_ambigs_training", &b) && b) || - (api.GetBoolVariable("tessedit_resegment_from_boxes", &b) && b) || - (api.GetBoolVariable("tessedit_make_boxes_from_boxes", &b) && b); + (api.GetBoolVariable("tessedit_ambigs_training", &b) && b) || + (api.GetBoolVariable("tessedit_resegment_from_boxes", &b) && b) || + (api.GetBoolVariable("tessedit_make_boxes_from_boxes", &b) && b); tesseract::PointerVector renderers; - - if (in_training_mode) { renderers.push_back(NULL); } else { diff --git a/ccmain/control.cpp b/ccmain/control.cpp index 3970c5429..79a0c27f3 100644 --- a/ccmain/control.cpp +++ b/ccmain/control.cpp @@ -1,8 +1,8 @@ /****************************************************************** * File: control.cpp (Formerly control.c) * Description: Module-independent matcher controller. - * Author: Ray Smith - * Created: Thu Apr 23 11:09:58 BST 1992 + * Author: Ray Smith + * Created: Thu Apr 23 11:09:58 BST 1992 * ReHacked: Tue Sep 22 08:42:49 BST 1992 Phil Cheatle * * (C) Copyright 1992, Hewlett-Packard Ltd. @@ -73,7 +73,6 @@ void Tesseract::recog_pseudo_word(PAGE_RES* page_res, } } - /** * Recognize a single word in interactive mode. * @@ -539,7 +538,7 @@ void Tesseract::bigram_correction_pass(PAGE_RES *page_res) { } } } - if (overrides_word1.size() >= 1) { + if (!overrides_word1.empty()) { // Excellent, we have some bigram matches. if (EqualIgnoringCaseAndTerminalPunct(*w_prev->best_choice, *overrides_word1[best_idx]) && diff --git a/ccmain/docqual.cpp b/ccmain/docqual.cpp index c6e7f17e0..4706fb3b2 100644 --- a/ccmain/docqual.cpp +++ b/ccmain/docqual.cpp @@ -1,8 +1,8 @@ /****************************************************************** * File: docqual.cpp (Formerly docqual.c) * Description: Document Quality Metrics - * Author: Phil Cheatle - * Created: Mon May 9 11:27:28 BST 1994 + * Author: Phil Cheatle + * Created: Mon May 9 11:27:28 BST 1994 * * (C) Copyright 1994, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -98,8 +98,8 @@ void Tesseract::word_char_quality(WERD_RES *word, ROW *row, inT16 *match_count, inT16 *accepted_match_count) { - if (word->bln_boxes == NULL || - word->rebuild_word == NULL || word->rebuild_word->blobs.empty()) { + if (word->bln_boxes == NULL || word->rebuild_word == NULL || + word->rebuild_word->blobs.empty()) { *match_count = 0; *accepted_match_count = 0; return; @@ -132,7 +132,7 @@ inT16 Tesseract::count_outline_errs(char c, inT16 outline_count) { int expected_outline_count; if (STRING (outlines_odd).contains (c)) - return 0; //Don't use this char + return 0; // Don't use this char else if (STRING (outlines_2).contains (c)) expected_outline_count = 2; else @@ -151,17 +151,16 @@ void Tesseract::quality_based_rejection(PAGE_RES_IT &page_res_it, } } - /************************************************************************* * unrej_good_quality_words() * Accept potential rejects in words which pass the following checks: * - Contains a potential reject * - Word looks like a sensible alpha word. * - Word segmentation is the same as the original image - * - All characters have the expected number of outlines + * - All characters have the expected number of outlines * NOTE - the rejection counts are recalculated after unrejection * - CAN'T do it in a single pass without a bit of fiddling - * - keep it simple but inefficient + * - keep it simple but inefficient *************************************************************************/ void Tesseract::unrej_good_quality_words( //unreject potential PAGE_RES_IT &page_res_it) { @@ -403,7 +402,6 @@ void Tesseract::doc_and_block_rejection( //reject big chunks } // namespace tesseract - /************************************************************************* * reject_whole_page() * Don't believe any of it - set the reject map to 00..00 in all words diff --git a/ccmain/fixspace.cpp b/ccmain/fixspace.cpp index f58c9610f..5fbe8c9a4 100644 --- a/ccmain/fixspace.cpp +++ b/ccmain/fixspace.cpp @@ -3,8 +3,8 @@ * Description: Implements a pass over the page res, exploring the alternative * spacing possibilities, trying to use context to improve the * word spacing -* Author: Phil Cheatle -* Created: Thu Oct 21 11:38:43 BST 1993 +* Author: Phil Cheatle +* Created: Thu Oct 21 11:38:43 BST 1993 * * (C) Copyright 1993, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -211,7 +211,6 @@ void Tesseract::match_current_words(WERD_RES_LIST &words, ROW *row, } } - /** * @name eval_word_spacing() * The basic measure is the number of characters in contextually confirmed diff --git a/ccmain/ltrresultiterator.cpp b/ccmain/ltrresultiterator.cpp index d5b859466..f80e59451 100644 --- a/ccmain/ltrresultiterator.cpp +++ b/ccmain/ltrresultiterator.cpp @@ -145,13 +145,12 @@ float LTRResultIterator::Confidence(PageIteratorLevel level) const { return 0.0f; } -void LTRResultIterator::RowAttributes(float* row_height, - float* descenders, +void LTRResultIterator::RowAttributes(float* row_height, float* descenders, float* ascenders) const { - *row_height = it_->row()->row->x_height() + it_->row()-> row->ascenders() - - it_->row()->row->descenders(); - *descenders = it_->row()->row->descenders(); - *ascenders = it_->row()->row->ascenders(); + *row_height = it_->row()->row->x_height() + it_->row()->row->ascenders() - + it_->row()->row->descenders(); + *descenders = it_->row()->row->descenders(); + *ascenders = it_->row()->row->ascenders(); } // Returns the font attributes of the current word. If iterating at a higher diff --git a/ccmain/ltrresultiterator.h b/ccmain/ltrresultiterator.h index 8819c2a0e..f2605b52d 100644 --- a/ccmain/ltrresultiterator.h +++ b/ccmain/ltrresultiterator.h @@ -92,8 +92,7 @@ class TESS_API LTRResultIterator : public PageIterator { float Confidence(PageIteratorLevel level) const; // Returns the attributes of the current row. - void RowAttributes(float* row_height, - float* descenders, + void RowAttributes(float* row_height, float* descenders, float* ascenders) const; // ============= Functions that refer to words only ============. diff --git a/ccmain/output.cpp b/ccmain/output.cpp index ddfcfc54b..6fca63e42 100644 --- a/ccmain/output.cpp +++ b/ccmain/output.cpp @@ -1,8 +1,8 @@ /****************************************************************** * File: output.cpp (Formerly output.c) * Description: Output pass - * Author: Phil Cheatle - * Created: Thu Aug 4 10:56:08 BST 1994 + * Author: Phil Cheatle + * Created: Thu Aug 4 10:56:08 BST 1994 * * (C) Copyright 1994, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -78,18 +78,16 @@ void Tesseract::output_pass( //Tess output pass //send to api while (page_res_it.word () != NULL) { check_debug_pt (page_res_it.word (), 120); - if (target_word_box) - { - - TBOX current_word_box=page_res_it.word ()->word->bounding_box(); - FCOORD center_pt((current_word_box.right()+current_word_box.left())/2,(current_word_box.bottom()+current_word_box.top())/2); - if (!target_word_box->contains(center_pt)) - { - page_res_it.forward (); - continue; - } - - } + if (target_word_box) { + TBOX current_word_box = page_res_it.word()->word->bounding_box(); + FCOORD center_pt( + (current_word_box.right() + current_word_box.left()) / 2, + (current_word_box.bottom() + current_word_box.top()) / 2); + if (!target_word_box->contains(center_pt)) { + page_res_it.forward(); + continue; + } + } if (tessedit_write_block_separators && block_of_last_word != page_res_it.block ()) { block_of_last_word = page_res_it.block (); @@ -337,7 +335,7 @@ void Tesseract::set_unlv_suspects(WERD_RES *word_res) { rating_per_ch = word.rating() / word_res->reject_map.length(); if (rating_per_ch >= suspect_rating_per_ch) - return; //Don't touch bad ratings + return; // Don't touch bad ratings if ((word_res->tess_accepted) || (rating_per_ch < suspect_accept_rating)) { /* Unreject any Tess Acceptable word - but NOT tess reject chs*/ diff --git a/ccmain/pagesegmain.cpp b/ccmain/pagesegmain.cpp index 4e3c34207..f4401d64d 100644 --- a/ccmain/pagesegmain.cpp +++ b/ccmain/pagesegmain.cpp @@ -412,9 +412,10 @@ ColumnFinder* Tesseract::SetupPageSegAndDetectOrientation( "Don't rotate.\n", osd_margin); osd_orientation = 0; } else { - tprintf("OSD: Weak margin (%.2f) for %d blob text block, " - "but using orientation anyway: %d\n", - osd_margin, osd_blobs.length(), osd_orientation); + tprintf( + "OSD: Weak margin (%.2f) for %d blob text block, " + "but using orientation anyway: %d\n", + osd_margin, osd_blobs.length(), osd_orientation); } } } diff --git a/ccmain/paragraphs.cpp b/ccmain/paragraphs.cpp index 7459940dd..c7d21a919 100644 --- a/ccmain/paragraphs.cpp +++ b/ccmain/paragraphs.cpp @@ -2052,7 +2052,7 @@ void ConvertHypothesizedModelRunsToParagraphs( bool single_line_paragraph = false; SetOfModels models; rows[start].NonNullHypotheses(&models); - if (models.size() > 0) { + if (!models.empty()) { model = models[0]; if (rows[start].GetLineType(model) != LT_BODY) single_line_paragraph = true; @@ -2190,17 +2190,17 @@ void LeftoverSegments(const GenericVector &rows, SetOfModels models_w_crowns; rows[i].StrongHypotheses(&models); rows[i].NonNullHypotheses(&models_w_crowns); - if (models.empty() && models_w_crowns.size() > 0) { + if (models.empty() && !models_w_crowns.empty()) { // Crown paragraph. Is it followed by a modeled line? for (int end = i + 1; end < rows.size(); end++) { SetOfModels end_models; SetOfModels strong_end_models; rows[end].NonNullHypotheses(&end_models); rows[end].StrongHypotheses(&strong_end_models); - if (end_models.size() == 0) { + if (end_models.empty()) { needs_fixing = true; break; - } else if (strong_end_models.size() > 0) { + } else if (!strong_end_models.empty()) { needs_fixing = false; break; } @@ -2485,7 +2485,7 @@ void InitializeRowInfo(bool after_recognition, info->ltr = ltr >= rtl; info->has_leaders = num_leaders > 3; info->num_words = werds.size(); - if (werds.size() > 0) { + if (!werds.empty()) { WERD_RES *lword = werds[0], *rword = werds[werds.size() - 1]; info->lword_text = lword->best_choice->unichar_string().string(); info->rword_text = rword->best_choice->unichar_string().string(); @@ -2538,7 +2538,7 @@ void DetectParagraphs(int debug_level, // If we're called before text recognition, we might not have // tight block bounding boxes, so trim by the minimum on each side. - if (row_infos.size() > 0) { + if (!row_infos.empty()) { int min_lmargin = row_infos[0].pix_ldistance; int min_rmargin = row_infos[0].pix_rdistance; for (int i = 1; i < row_infos.size(); i++) { diff --git a/ccmain/paramsd.cpp b/ccmain/paramsd.cpp index 7784f8536..e0e60539f 100644 --- a/ccmain/paramsd.cpp +++ b/ccmain/paramsd.cpp @@ -329,13 +329,19 @@ void ParamsEditor::WriteParams(char *filename, fclose(fp); sprintf (msg_str, "Overwrite file " "%s" "? (Y/N)", filename); int a = sv_window_->ShowYesNoDialog(msg_str); - if (a == 'n') { return; } // don't write + if (a == 'n') { + return; + } // don't write } fp = fopen (filename, "wb"); // can we write to it? if (fp == NULL) { - sv_window_->AddMessage("Can't write to file " "%s" "", filename); + sv_window_->AddMessage( + "Can't write to file " + "%s" + "", + filename); return; } diff --git a/ccmain/reject.cpp b/ccmain/reject.cpp index aacc80dd6..72f9d873d 100644 --- a/ccmain/reject.cpp +++ b/ccmain/reject.cpp @@ -1,8 +1,8 @@ /********************************************************************** * File: reject.cpp (Formerly reject.c) * Description: Rejection functions used in tessedit - * Author: Phil Cheatle - * Created: Wed Sep 23 16:50:21 BST 1992 + * Author: Phil Cheatle + * Created: Wed Sep 23 16:50:21 BST 1992 * * (C) Copyright 1992, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/ccmain/tessedit.cpp b/ccmain/tessedit.cpp index dd96ba0eb..8c1fb8083 100644 --- a/ccmain/tessedit.cpp +++ b/ccmain/tessedit.cpp @@ -44,7 +44,7 @@ #include "params.h" #define VARDIR "configs/" /*variables files */ - //config under api + // config under api #define API_CONFIG "configs/api_config" ETEXT_DESC *global_monitor = NULL; // progress monitor diff --git a/ccmain/tesseractclass.cpp b/ccmain/tesseractclass.cpp index 8db50fbd5..f0cc1bfff 100644 --- a/ccmain/tesseractclass.cpp +++ b/ccmain/tesseractclass.cpp @@ -398,8 +398,8 @@ Tesseract::Tesseract() "Don't suspect dict wds longer than this", this->params()), BOOL_MEMBER(suspect_constrain_1Il, false, "UNLV keep 1Il chars rejected", this->params()), - double_MEMBER(suspect_rating_per_ch, 999.9, "Don't touch bad rating limit", - this->params()), + double_MEMBER(suspect_rating_per_ch, 999.9, + "Don't touch bad rating limit", this->params()), double_MEMBER(suspect_accept_rating, -999.9, "Accept good rating limit", this->params()), BOOL_MEMBER(tessedit_minimal_rejection, false, @@ -512,7 +512,6 @@ Tesseract::Tesseract() "Page separator (default is form feed control character)", this->params()), - // The following parameters were deprecated and removed from their // original // locations. The parameters are temporarily kept here to give Tesseract diff --git a/ccmain/tesseractclass.h b/ccmain/tesseractclass.h index 91d25bc8a..e05eac502 100644 --- a/ccmain/tesseractclass.h +++ b/ccmain/tesseractclass.h @@ -38,7 +38,6 @@ class BLOB_CHOICE_LIST_CLIST; class BLOCK_LIST; -class CharSamp; struct OSResults; class PAGE_RES; class PAGE_RES_IT; @@ -98,6 +97,7 @@ namespace tesseract { class ColumnFinder; #ifndef NO_CUBE_BUILD +class CharSamp; class CubeLineObject; class CubeObject; class CubeRecoContext; @@ -1010,8 +1010,7 @@ class Tesseract : public Wordrec { INT_VAR_H(suspect_level, 99, "Suspect marker level"); INT_VAR_H(suspect_space_level, 100, "Min suspect level for rejecting spaces"); - INT_VAR_H(suspect_short_words, 2, - "Don't Suspect dict wds longer than this"); + INT_VAR_H(suspect_short_words, 2, "Don't Suspect dict wds longer than this"); BOOL_VAR_H(suspect_constrain_1Il, false, "UNLV keep 1Il chars rejected"); double_VAR_H(suspect_rating_per_ch, 999.9, "Don't touch bad rating limit"); double_VAR_H(suspect_accept_rating, -999.9, "Accept good rating limit"); diff --git a/ccstruct/blobbox.cpp b/ccstruct/blobbox.cpp index 280096b5d..3ffb9dc93 100644 --- a/ccstruct/blobbox.cpp +++ b/ccstruct/blobbox.cpp @@ -1,8 +1,8 @@ /********************************************************************** * File: blobbox.cpp (Formerly blobnbox.c) * Description: Code for the textord blob class. - * Author: Ray Smith - * Created: Thu Jul 30 09:08:51 BST 1992 + * Author: Ray Smith + * Created: Thu Jul 30 09:08:51 BST 1992 * * (C) Copyright 1992, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -31,7 +31,9 @@ #define PROJECTION_MARGIN 10 //arbitrary #define EXTERN -ELISTIZE (BLOBNBOX) ELIST2IZE (TO_ROW) ELISTIZE (TO_BLOCK) +ELISTIZE(BLOBNBOX) +ELIST2IZE(TO_ROW) +ELISTIZE(TO_BLOCK) // Up to 30 degrees is allowed for rotations of diacritic blobs. const double kCosSmallAngle = 0.866; @@ -176,7 +178,7 @@ void BLOBNBOX::NeighbourGaps(int gaps[BND_COUNT]) const { gaps[dir] = MAX_INT16; BLOBNBOX* neighbour = neighbours_[dir]; if (neighbour != NULL) { - TBOX n_box = neighbour->bounding_box(); + const TBOX& n_box = neighbour->bounding_box(); if (dir == BND_LEFT || dir == BND_RIGHT) { gaps[dir] = box.x_gap(n_box); } else { diff --git a/ccstruct/boxread.cpp b/ccstruct/boxread.cpp index f4aedca5b..fee0aa9ae 100644 --- a/ccstruct/boxread.cpp +++ b/ccstruct/boxread.cpp @@ -34,8 +34,7 @@ FILE* OpenBoxFile(const STRING& fname) { STRING filename = BoxFileName(fname); FILE* box_file = NULL; if (!(box_file = fopen(filename.string(), "rb"))) { - CANTOPENFILE.error("read_next_box", TESSEXIT, - "Can't open box file %s", + CANTOPENFILE.error("read_next_box", TESSEXIT, "Can't open box file %s", filename.string()); } return box_file; diff --git a/ccstruct/boxword.h b/ccstruct/boxword.h index 742bbb8e4..c1fab068b 100644 --- a/ccstruct/boxword.h +++ b/ccstruct/boxword.h @@ -82,9 +82,7 @@ class BoxWord { const TBOX& bounding_box() const { return bbox_; } - int length() const { - return length_; - } + int length() const { return length_; } const TBOX& BlobBox(int index) const { return boxes_[index]; } diff --git a/ccstruct/coutln.cpp b/ccstruct/coutln.cpp index bc2b119d8..238272d2c 100644 --- a/ccstruct/coutln.cpp +++ b/ccstruct/coutln.cpp @@ -48,9 +48,9 @@ ICOORD C_OUTLINE::step_coords[4] = { * @param length length of loop */ -C_OUTLINE::C_OUTLINE (CRACKEDGE * startpt, ICOORD bot_left, - ICOORD top_right, inT16 length) - : box (bot_left, top_right), start (startpt->pos), offsets(NULL) { +C_OUTLINE::C_OUTLINE(CRACKEDGE* startpt, ICOORD bot_left, ICOORD top_right, + inT16 length) + : box(bot_left, top_right), start(startpt->pos), offsets(NULL) { inT16 stepindex; //index to step CRACKEDGE *edgept; //current point @@ -71,7 +71,6 @@ C_OUTLINE::C_OUTLINE (CRACKEDGE * startpt, ICOORD bot_left, } } - /** * @name C_OUTLINE::C_OUTLINE * @@ -139,7 +138,7 @@ inT16 length //length of loop * @param rotation rotate to coord */ -C_OUTLINE::C_OUTLINE(C_OUTLINE *srcline, FCOORD rotation) : offsets(NULL) { +C_OUTLINE::C_OUTLINE(C_OUTLINE* srcline, FCOORD rotation) : offsets(NULL) { TBOX new_box; //easy bounding inT16 stepindex; //index to step inT16 dirdiff; //direction change @@ -300,7 +299,6 @@ inT32 C_OUTLINE::perimeter() const { return total_steps; } - /** * @name C_OUTLINE::outer_area * @@ -332,7 +330,6 @@ inT32 C_OUTLINE::outer_area() const { return total; } - /** * @name C_OUTLINE::count_transitions * @@ -459,7 +456,6 @@ inT32 C_OUTLINE::count_transitions(inT32 threshold) { return total; } - /** * @name C_OUTLINE::operator< * @@ -468,8 +464,7 @@ inT32 C_OUTLINE::count_transitions(inT32 threshold) { */ BOOL8 -C_OUTLINE::operator< (const C_OUTLINE & other) const -{ +C_OUTLINE::operator<(const C_OUTLINE& other) const { inT16 count = 0; //winding count ICOORD pos; //position of point inT32 stepindex; //index to cstep @@ -495,7 +490,6 @@ C_OUTLINE::operator< (const C_OUTLINE & other) const return count != 0; } - /** * @name C_OUTLINE::winding_number * @@ -534,7 +528,6 @@ inT16 C_OUTLINE::winding_number(ICOORD point) const { return count; //winding number } - /** * C_OUTLINE::turn_direction * @@ -563,7 +556,6 @@ inT16 C_OUTLINE::turn_direction() const { //winding number return count; //winding number } - /** * @name C_OUTLINE::reverse * @@ -586,7 +578,6 @@ void C_OUTLINE::reverse() { //reverse drection } } - /** * @name C_OUTLINE::move * @@ -661,14 +652,27 @@ static void ComputeGradient(const l_uint32* data, int wpl, int x, int y, int width, int height, ICOORD* gradient) { const l_uint32* line = data + y * wpl; - int pix_x_y = x < width && y < height ? - GET_DATA_BYTE(const_cast (reinterpret_cast(line)), x) : 255; - int pix_x_prevy = x < width && y > 0 ? - GET_DATA_BYTE(const_cast (reinterpret_cast(line - wpl)), x) : 255; - int pix_prevx_prevy = x > 0 && y > 0 ? - GET_DATA_BYTE(const_cast (reinterpret_cast(line - wpl)), x - 1) : 255; - int pix_prevx_y = x > 0 && y < height ? - GET_DATA_BYTE(const_cast (reinterpret_cast(line)), x - 1) : 255; + int pix_x_y = + x < width && y < height + ? GET_DATA_BYTE( + const_cast(reinterpret_cast(line)), x) + : 255; + int pix_x_prevy = + x < width && y > 0 + ? GET_DATA_BYTE( + const_cast(reinterpret_cast(line - wpl)), x) + : 255; + int pix_prevx_prevy = + x > 0 && y > 0 + ? GET_DATA_BYTE( + const_cast(reinterpret_cast(line - wpl)), + x - 1) + : 255; + int pix_prevx_y = + x > 0 && y < height + ? GET_DATA_BYTE( + const_cast(reinterpret_cast(line)), x - 1) + : 255; gradient->set_x(pix_x_y + pix_x_prevy - (pix_prevx_y + pix_prevx_prevy)); gradient->set_y(pix_x_prevy + pix_prevx_prevy - (pix_x_y + pix_prevx_y)); } @@ -684,8 +688,10 @@ static bool EvaluateVerticalDiff(const l_uint32* data, int wpl, int diff_sign, if (y <= 0 || y >= height) return false; const l_uint32* line = data + y * wpl; - int pixel1 = GET_DATA_BYTE(const_cast (reinterpret_cast(line - wpl)), x); - int pixel2 = GET_DATA_BYTE(const_cast (reinterpret_cast(line)), x); + int pixel1 = GET_DATA_BYTE( + const_cast(reinterpret_cast(line - wpl)), x); + int pixel2 = + GET_DATA_BYTE(const_cast(reinterpret_cast(line)), x); int diff = (pixel2 - pixel1) * diff_sign; if (diff > *best_diff) { *best_diff = diff; @@ -705,8 +711,10 @@ static bool EvaluateHorizontalDiff(const l_uint32* line, int diff_sign, int* best_diff, int* best_sum, int* best_x) { if (x <= 0 || x >= width) return false; - int pixel1 = GET_DATA_BYTE(const_cast (reinterpret_cast(line)), x - 1); - int pixel2 = GET_DATA_BYTE(const_cast (reinterpret_cast(line)), x); + int pixel1 = GET_DATA_BYTE( + const_cast(reinterpret_cast(line)), x - 1); + int pixel2 = + GET_DATA_BYTE(const_cast(reinterpret_cast(line)), x); int diff = (pixel2 - pixel1) * diff_sign; if (diff > *best_diff) { *best_diff = diff; @@ -954,8 +962,7 @@ void C_OUTLINE::render_outline(int left, int top, Pix* pix) const { */ #ifndef GRAPHICS_DISABLED -void C_OUTLINE::plot(ScrollView* window, - ScrollView::Color colour) const { +void C_OUTLINE::plot(ScrollView* window, ScrollView::Color colour) const { inT16 stepindex; // index to cstep ICOORD pos; // current position DIR128 stepdir; // direction of step @@ -1016,7 +1023,6 @@ void C_OUTLINE::plot_normed(const DENORM& denorm, ScrollView::Color colour, } #endif - /** * @name C_OUTLINE::operator= * @@ -1024,7 +1030,7 @@ void C_OUTLINE::plot_normed(const DENORM& denorm, ScrollView::Color colour, * @param source assign from this */ -C_OUTLINE & C_OUTLINE::operator= (const C_OUTLINE & source) { +C_OUTLINE& C_OUTLINE::operator=(const C_OUTLINE& source) { box = source.box; start = source.start; if (steps != NULL) diff --git a/ccstruct/mod128.cpp b/ccstruct/mod128.cpp index ee4aa6c3c..4e5f4bd32 100644 --- a/ccstruct/mod128.cpp +++ b/ccstruct/mod128.cpp @@ -1,8 +1,8 @@ /********************************************************************** * File: mod128.c (Formerly dir128.c) * Description: Code to convert a DIR128 to an ICOORD. - * Author: Ray Smith - * Created: Tue Oct 22 11:56:09 BST 1991 + * Author: Ray Smith + * Created: Tue Oct 22 11:56:09 BST 1991 * * (C) Copyright 1991, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -86,16 +86,3 @@ DIR128::DIR128( //from fcoord while (high - low > 1); dir = low; } - - -/********************************************************************** - * dir_to_gradient - * - * Convert a direction to a vector. - **********************************************************************/ - -#if 0 // code is buggy for negative dir and unused -ICOORD DIR128::vector() const { //convert to vector - return dirtab[dir]; //easy really -} -#endif diff --git a/ccstruct/mod128.h b/ccstruct/mod128.h index 592264ba6..c0e71a423 100644 --- a/ccstruct/mod128.h +++ b/ccstruct/mod128.h @@ -1,8 +1,8 @@ /********************************************************************** * File: mod128.h (Formerly dir128.h) * Description: Header for class which implements modulo arithmetic. - * Author: Ray Smith - * Created: Tue Mar 26 17:48:13 GMT 1991 + * Author: Ray Smith + * Created: Tue Mar 26 17:48:13 GMT 1991 * * (C) Copyright 1991, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -77,7 +77,6 @@ class DLLSYM DIR128 inT8 get_dir() const { //access function return dir; } - ICOORD vector() const; //turn to vector private: inT8 dir; //a direction diff --git a/ccstruct/otsuthr.cpp b/ccstruct/otsuthr.cpp index 071b0d234..ee6c73992 100644 --- a/ccstruct/otsuthr.cpp +++ b/ccstruct/otsuthr.cpp @@ -51,23 +51,16 @@ int OtsuThreshold(Pix* src_pix, int left, int top, int width, int height, // only use opencl if compiled w/ OpenCL and selected device is opencl #ifdef USE_OPENCL - // all of channel 0 then all of channel 1... - int *histogramAllChannels = new int[kHistogramSize * num_channels]; + // all of channel 0 then all of channel 1... + int* histogramAllChannels = new int[kHistogramSize * num_channels]; - // Calculate Histogram on GPU - OpenclDevice od; - if (od.selectedDeviceIsOpenCL() && - (num_channels == 1 || num_channels == 4) && top == 0 && left == 0 ) { - od.HistogramRectOCL( - (const unsigned char*)pixGetData(src_pix), - num_channels, - pixGetWpl(src_pix) * 4, - left, - top, - width, - height, - kHistogramSize, - histogramAllChannels); + // Calculate Histogram on GPU + OpenclDevice od; + if (od.selectedDeviceIsOpenCL() && (num_channels == 1 || num_channels == 4) && + top == 0 && left == 0) { + od.HistogramRectOCL((const unsigned char*)pixGetData(src_pix), num_channels, + pixGetWpl(src_pix) * 4, left, top, width, height, + kHistogramSize, histogramAllChannels); // Calculate Threshold from Histogram on cpu for (int ch = 0; ch < num_channels; ++ch) { @@ -143,7 +136,6 @@ int OtsuThreshold(Pix* src_pix, int left, int top, int width, int height, delete[] histogramAllChannels; #endif // USE_OPENCL - if (!any_good_hivalue) { // Use the best of the ones that were not good enough. (*hi_values)[best_hi_index] = best_hi_value; diff --git a/ccstruct/pageres.h b/ccstruct/pageres.h index 7329bc89e..22c5ccb77 100644 --- a/ccstruct/pageres.h +++ b/ccstruct/pageres.h @@ -1,7 +1,7 @@ /********************************************************************** * File: pageres.h (Formerly page_res.h) * Description: Results classes used by control.c - * Author: Phil Cheatle + * Author: Phil Cheatle * Created: Tue Sep 22 08:42:49 BST 1992 * * (C) Copyright 1992, Hewlett-Packard Ltd. @@ -327,7 +327,7 @@ class WERD_RES : public ELIST_LINK { } // Deep copies everything except the ratings MATRIX. // To get that use deep_copy below. - WERD_RES(const WERD_RES &source) : ELIST_LINK(source) { + WERD_RES(const WERD_RES& source) : ELIST_LINK(source) { InitPointers(); *this = source; // see operator= } @@ -630,7 +630,7 @@ class WERD_RES : public ELIST_LINK { static WERD_RES* deep_copy(const WERD_RES* src) { WERD_RES* result = new WERD_RES(*src); // That didn't copy the ratings, but we want a copy if there is one to - // begin width. + // begin with. if (src->ratings != NULL) result->ratings = src->ratings->DeepCopy(); return result; diff --git a/ccstruct/pdblock.h b/ccstruct/pdblock.h index b64eff36d..e9139f2ac 100644 --- a/ccstruct/pdblock.h +++ b/ccstruct/pdblock.h @@ -29,90 +29,76 @@ struct Pix; CLISTIZEH (PDBLK) ///page block -class PDBLK -{ +class PDBLK { friend class BLOCK_RECT_IT; //< block iterator - public: - ///empty constructor - PDBLK() { - hand_poly = NULL; - index_ = 0; - } - ///simple constructor - PDBLK(inT16 xmin, //< bottom left - inT16 ymin, - inT16 xmax, //< top right - inT16 ymax); + public: + /// empty constructor + PDBLK() { + hand_poly = NULL; + index_ = 0; + } + /// simple constructor + PDBLK(inT16 xmin, //< bottom left + inT16 ymin, + inT16 xmax, //< top right + inT16 ymax); - ///set vertex lists - ///@param left list of left vertices - ///@param right list of right vertices - void set_sides(ICOORDELT_LIST *left, - ICOORDELT_LIST *right); + /// set vertex lists + ///@param left list of left vertices + ///@param right list of right vertices + void set_sides(ICOORDELT_LIST *left, ICOORDELT_LIST *right); - ///destructor - ~PDBLK () { - if (hand_poly) delete hand_poly; - } + /// destructor + ~PDBLK() { + if (hand_poly) delete hand_poly; + } - POLY_BLOCK *poly_block() const { - return hand_poly; - } - ///set the poly block - void set_poly_block(POLY_BLOCK *blk) { - hand_poly = blk; - } - ///get box - void bounding_box(ICOORD &bottom_left, //bottom left - ICOORD &top_right) const { //topright - bottom_left = box.botleft (); - top_right = box.topright (); - } - ///get real box - const TBOX &bounding_box() const { - return box; - } + POLY_BLOCK *poly_block() const { return hand_poly; } + /// set the poly block + void set_poly_block(POLY_BLOCK *blk) { hand_poly = blk; } + /// get box + void bounding_box(ICOORD &bottom_left, // bottom left + ICOORD &top_right) const { // topright + bottom_left = box.botleft(); + top_right = box.topright(); + } + /// get real box + const TBOX &bounding_box() const { return box; } - int index() const { - return index_; - } - void set_index(int value) { - index_ = value; - } + int index() const { return index_; } + void set_index(int value) { index_ = value; } - ///is pt inside block - BOOL8 contains(ICOORD pt); + /// is pt inside block + BOOL8 contains(ICOORD pt); - /// reposition block - void move(const ICOORD vec); // by vector + /// reposition block + void move(const ICOORD vec); // by vector - // Returns a binary Pix mask with a 1 pixel for every pixel within the - // block. Rotates the coordinate system by rerotation prior to rendering. - // If not NULL, mask_box is filled with the position box of the returned - // mask image. - Pix *render_mask(const FCOORD &rerotation, TBOX *mask_box); + // Returns a binary Pix mask with a 1 pixel for every pixel within the + // block. Rotates the coordinate system by rerotation prior to rendering. + // If not NULL, mask_box is filled with the position box of the returned + // mask image. + Pix *render_mask(const FCOORD &rerotation, TBOX *mask_box); - #ifndef GRAPHICS_DISABLED - ///draw histogram - ///@param window window to draw in - ///@param serial serial number - ///@param colour colour to draw in - void plot(ScrollView* window, - inT32 serial, - ScrollView::Color colour); - #endif // GRAPHICS_DISABLED +#ifndef GRAPHICS_DISABLED + /// draw histogram + ///@param window window to draw in + ///@param serial serial number + ///@param colour colour to draw in + void plot(ScrollView *window, inT32 serial, ScrollView::Color colour); +#endif // GRAPHICS_DISABLED - ///assignment - ///@param source from this - PDBLK & operator= (const PDBLK & source); + /// assignment + ///@param source from this + PDBLK &operator=(const PDBLK &source); - protected: - POLY_BLOCK *hand_poly; //< weird as well - ICOORDELT_LIST leftside; //< left side vertices - ICOORDELT_LIST rightside; //< right side vertices - TBOX box; //< bounding box - int index_; //< Serial number of this block. + protected: + POLY_BLOCK *hand_poly; //< weird as well + ICOORDELT_LIST leftside; //< left side vertices + ICOORDELT_LIST rightside; //< right side vertices + TBOX box; //< bounding box + int index_; //< Serial number of this block. }; class DLLSYM BLOCK_RECT_IT //rectangle iterator diff --git a/ccstruct/polyaprx.cpp b/ccstruct/polyaprx.cpp index 81b8500a0..759734954 100644 --- a/ccstruct/polyaprx.cpp +++ b/ccstruct/polyaprx.cpp @@ -214,7 +214,7 @@ EDGEPT edgepts[] //output is array void fix2( //polygonal approx EDGEPT *start, /*loop to approimate */ int area) { - EDGEPT *edgept; /*current point */ + EDGEPT *edgept; /*current point */ EDGEPT *edgept1; EDGEPT *loopstart; /*modified start of loop */ EDGEPT *linestart; /*start of line segment */ diff --git a/ccstruct/polyblk.cpp b/ccstruct/polyblk.cpp index e0a455905..b5ca2e121 100644 --- a/ccstruct/polyblk.cpp +++ b/ccstruct/polyblk.cpp @@ -1,7 +1,7 @@ /********************************************************************** * File: polyblk.c (Formerly poly_block.c) * Description: Polygonal blocks - * Author: Sheelagh Lloyd? + * Author: Sheelagh Lloyd? * Created: * * (C) Copyright 1993, Hewlett-Packard Ltd. diff --git a/ccstruct/quspline.cpp b/ccstruct/quspline.cpp index f50cfe50e..82107e1e2 100644 --- a/ccstruct/quspline.cpp +++ b/ccstruct/quspline.cpp @@ -1,8 +1,8 @@ /********************************************************************** * File: quspline.cpp (Formerly qspline.c) * Description: Code for the QSPLINE class. - * Author: Ray Smith - * Created: Tue Oct 08 17:16:12 BST 1991 + * Author: Ray Smith + * Created: Tue Oct 08 17:16:12 BST 1991 * * (C) Copyright 1991, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/ccstruct/ratngs.h b/ccstruct/ratngs.h index 31b27cfb3..446dfc6c5 100644 --- a/ccstruct/ratngs.h +++ b/ccstruct/ratngs.h @@ -288,7 +288,8 @@ class WERD_CHOICE : public ELIST_LINK { src_certainty, src_permuter); } WERD_CHOICE(const char *src_string, const UNICHARSET &unicharset); - WERD_CHOICE(const WERD_CHOICE &word) : ELIST_LINK(word), unicharset_(word.unicharset_) { + WERD_CHOICE(const WERD_CHOICE &word) + : ELIST_LINK(word), unicharset_(word.unicharset_) { this->init(word.length()); this->operator=(word); } diff --git a/ccstruct/rect.cpp b/ccstruct/rect.cpp index 22417485d..4a9fe00b3 100644 --- a/ccstruct/rect.cpp +++ b/ccstruct/rect.cpp @@ -1,8 +1,8 @@ /********************************************************************** * File: rect.c (Formerly box.c) * Description: Bounding box class definition. - * Author: Phil Cheatle - * Created: Wed Oct 16 15:18:45 BST 1991 + * Author: Phil Cheatle + * Created: Wed Oct 16 15:18:45 BST 1991 * * (C) Copyright 1991, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -29,10 +29,10 @@ * **********************************************************************/ -TBOX::TBOX( //constructor - const ICOORD pt1, //one corner - const ICOORD pt2 //the other corner - ) { +TBOX::TBOX( // constructor + const ICOORD pt1, // one corner + const ICOORD pt2 // the other corner + ) { if (pt1.x () <= pt2.x ()) { if (pt1.y () <= pt2.y ()) { bot_left = pt1; diff --git a/ccstruct/rect.h b/ccstruct/rect.h index d9b90642f..f31247a1a 100644 --- a/ccstruct/rect.h +++ b/ccstruct/rect.h @@ -1,8 +1,8 @@ /********************************************************************** * File: rect.h (Formerly box.h) * Description: Bounding box class definition. - * Author: Phil Cheatle - * Created: Wed Oct 16 15:18:45 BST 1991 + * Author: Phil Cheatle + * Created: Wed Oct 16 15:18:45 BST 1991 * * (C) Copyright 1991, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -307,9 +307,9 @@ class DLLSYM TBOX { // bounding box * **********************************************************************/ -inline TBOX::TBOX( // constructor - const FCOORD pt // floating centre - ) { +inline TBOX::TBOX( // constructor + const FCOORD pt // floating centre + ) { bot_left = ICOORD ((inT16) floor (pt.x ()), (inT16) floor (pt.y ())); top_right = ICOORD ((inT16) ceil (pt.x ()), (inT16) ceil (pt.y ())); } diff --git a/ccstruct/rejctmap.h b/ccstruct/rejctmap.h index d945dda1f..009ba58a7 100644 --- a/ccstruct/rejctmap.h +++ b/ccstruct/rejctmap.h @@ -1,8 +1,8 @@ /********************************************************************** * File: rejctmap.h (Formerly rejmap.h) * Description: REJ and REJMAP class functions. - * Author: Phil Cheatle - * Created: Thu Jun 9 13:46:38 BST 1994 + * Author: Phil Cheatle + * Created: Thu Jun 9 13:46:38 BST 1994 * * (C) Copyright 1994, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -48,46 +48,45 @@ OF THIS IMPLIED TEMPORAL ORDERING OF THE FLAGS!!!! #include "bits16.h" #include "params.h" -enum REJ_FLAGS -{ +enum REJ_FLAGS { /* Reject modes which are NEVER overridden */ - R_TESS_FAILURE, // PERM Tess didn't classify - R_SMALL_XHT, // PERM Xht too small - R_EDGE_CHAR, // PERM Too close to edge of image - R_1IL_CONFLICT, // PERM 1Il confusion - R_POSTNN_1IL, // PERM 1Il unrejected by NN - R_REJ_CBLOB, // PERM Odd blob - R_MM_REJECT, // PERM Matrix match rejection (m's) - R_BAD_REPETITION, // TEMP Repeated char which doesn't match trend + R_TESS_FAILURE, // PERM Tess didn't classify + R_SMALL_XHT, // PERM Xht too small + R_EDGE_CHAR, // PERM Too close to edge of image + R_1IL_CONFLICT, // PERM 1Il confusion + R_POSTNN_1IL, // PERM 1Il unrejected by NN + R_REJ_CBLOB, // PERM Odd blob + R_MM_REJECT, // PERM Matrix match rejection (m's) + R_BAD_REPETITION, // TEMP Repeated char which doesn't match trend /* Initial reject modes (pre NN_ACCEPT) */ - R_POOR_MATCH, // TEMP Ray's original heuristic (Not used) - R_NOT_TESS_ACCEPTED, // TEMP Tess didn't accept WERD - R_CONTAINS_BLANKS, // TEMP Tess failed on other chs in WERD - R_BAD_PERMUTER, // POTENTIAL Bad permuter for WERD + R_POOR_MATCH, // TEMP Ray's original heuristic (Not used) + R_NOT_TESS_ACCEPTED, // TEMP Tess didn't accept WERD + R_CONTAINS_BLANKS, // TEMP Tess failed on other chs in WERD + R_BAD_PERMUTER, // POTENTIAL Bad permuter for WERD /* Reject modes generated after NN_ACCEPT but before MM_ACCEPT */ - R_HYPHEN, // TEMP Post NN dodgy hyphen or full stop - R_DUBIOUS, // TEMP Post NN dodgy chars - R_NO_ALPHANUMS, // TEMP No alphanumerics in word after NN - R_MOSTLY_REJ, // TEMP Most of word rejected so rej the rest - R_XHT_FIXUP, // TEMP Xht tests unsure + R_HYPHEN, // TEMP Post NN dodgy hyphen or full stop + R_DUBIOUS, // TEMP Post NN dodgy chars + R_NO_ALPHANUMS, // TEMP No alphanumerics in word after NN + R_MOSTLY_REJ, // TEMP Most of word rejected so rej the rest + R_XHT_FIXUP, // TEMP Xht tests unsure /* Reject modes generated after MM_ACCEPT but before QUALITY_ACCEPT */ - R_BAD_QUALITY, // TEMP Quality metrics bad for WERD + R_BAD_QUALITY, // TEMP Quality metrics bad for WERD /* Reject modes generated after QUALITY_ACCEPT but before MINIMAL_REJ accep*/ - R_DOC_REJ, // TEMP Document rejection - R_BLOCK_REJ, // TEMP Block rejection - R_ROW_REJ, // TEMP Row rejection - R_UNLV_REJ, // TEMP ~ turned to - or ^ turned to space + R_DOC_REJ, // TEMP Document rejection + R_BLOCK_REJ, // TEMP Block rejection + R_ROW_REJ, // TEMP Row rejection + R_UNLV_REJ, // TEMP ~ turned to - or ^ turned to space /* Accept modes which occur between the above rejection groups */ - R_NN_ACCEPT, //NN acceptance - R_HYPHEN_ACCEPT, //Hyphen acceptance - R_MM_ACCEPT, //Matrix match acceptance - R_QUALITY_ACCEPT, //Accept word in good quality doc - R_MINIMAL_REJ_ACCEPT //Accept EVERYTHING except tess failures + R_NN_ACCEPT, // NN acceptance + R_HYPHEN_ACCEPT, // Hyphen acceptance + R_MM_ACCEPT, // Matrix match acceptance + R_QUALITY_ACCEPT, // Accept word in good quality doc + R_MINIMAL_REJ_ACCEPT // Accept EVERYTHING except tess failures }; /* REJECT MAP VALUES */ diff --git a/ccstruct/statistc.cpp b/ccstruct/statistc.cpp index 39d5edd18..8b1ba8c9a 100644 --- a/ccstruct/statistc.cpp +++ b/ccstruct/statistc.cpp @@ -1,8 +1,8 @@ /********************************************************************** * File: statistc.c (Formerly stats.c) * Description: Simple statistical package for integer values. - * Author: Ray Smith - * Created: Mon Feb 04 16:56:05 GMT 1991 + * Author: Ray Smith + * Created: Mon Feb 04 16:56:05 GMT 1991 * * (C) Copyright 1991, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -215,7 +215,6 @@ inT32 STATS::min_bucket() const { // Find min return rangemin_ + min; } - /********************************************************************** * STATS::max_bucket * diff --git a/ccutil/clst.cpp b/ccutil/clst.cpp index fbbb561fa..52caadf38 100644 --- a/ccutil/clst.cpp +++ b/ccutil/clst.cpp @@ -26,7 +26,7 @@ **********************************************************************/ /*********************************************************************** - * CLIST::internal_deep_clear + * CLIST::internal_deep_clear * * Used by the "deep_clear" member function of derived list * classes to destroy all the elements on the list. @@ -56,9 +56,8 @@ void (*zapper) (void *)) { //ptr to zapper functn } } - /*********************************************************************** - * CLIST::shallow_clear + * CLIST::shallow_clear * * Used by the destructor and the "shallow_clear" member function of derived * list classes to destroy the list. @@ -83,7 +82,7 @@ void CLIST::shallow_clear() { //destroy all links } /*********************************************************************** - * CLIST::assign_to_sublist + * CLIST::assign_to_sublist * * The list is set to a sublist of another list. "This" list must be empty * before this function is invoked. The two iterators passed must refer to @@ -107,9 +106,8 @@ void CLIST::assign_to_sublist( //to this list last = start_it->extract_sublist (end_it); } - /*********************************************************************** - * CLIST::length + * CLIST::length * * Return count of elements on list **********************************************************************/ @@ -123,9 +121,8 @@ inT32 CLIST::length() const { //count elements return count; } - /*********************************************************************** - * CLIST::sort + * CLIST::sort * * Sort elements on list **********************************************************************/ @@ -239,7 +236,7 @@ void CLIST::set_subtract(int comparator(const void*, const void*), **********************************************************************/ /*********************************************************************** - * CLIST_ITERATOR::forward + * CLIST_ITERATOR::forward * * Move the iterator to the next element of the list. * REMEMBER: ALL LISTS ARE CIRCULAR. @@ -276,9 +273,8 @@ void *CLIST_ITERATOR::forward() { return current->data; } - /*********************************************************************** - * CLIST_ITERATOR::data_relative + * CLIST_ITERATOR::data_relative * * Return the data pointer to the element "offset" elements from current. * "offset" must not be less than -1. @@ -312,9 +308,8 @@ void *CLIST_ITERATOR::data_relative( //get data + or - ... return ptr->data; } - /*********************************************************************** - * CLIST_ITERATOR::move_to_last() + * CLIST_ITERATOR::move_to_last() * * Move current so that it is set to the end of the list. * Return data just in case anyone wants it. @@ -336,9 +331,8 @@ void *CLIST_ITERATOR::move_to_last() { return current->data; } - /*********************************************************************** - * CLIST_ITERATOR::exchange() + * CLIST_ITERATOR::exchange() * * Given another iterator, whose current element is a different element on * the same list list OR an element of another list, exchange the two current @@ -434,9 +428,8 @@ void CLIST_ITERATOR::exchange( //positions of 2 link other_it->current = old_current; } - /*********************************************************************** - * CLIST_ITERATOR::extract_sublist() + * CLIST_ITERATOR::extract_sublist() * * This is a private member, used only by CLIST::assign_to_sublist. * Given another iterator for the same list, extract the links from THIS to @@ -478,7 +471,7 @@ CLIST_LINK *CLIST_ITERATOR::extract_sublist( //from temp_it.mark_cycle_pt (); do { //walk sublist - if (temp_it.cycled_list ()) //can't find end pt + if (temp_it.cycled_list()) // can't find end pt BAD_SUBLIST.error ("CLIST_ITERATOR.extract_sublist", ABORT, NULL); if (temp_it.at_last ()) { diff --git a/ccutil/clst.h b/ccutil/clst.h index a5a42a6e4..f93d75afc 100644 --- a/ccutil/clst.h +++ b/ccutil/clst.h @@ -28,9 +28,9 @@ class CLIST_ITERATOR; /********************************************************************** - * CLASS - CLIST_LINK + * CLASS - CLIST_LINK * - * Generic link class for singly linked CONS cell lists + * Generic link class for singly linked CONS cell lists * * Note: No destructor - elements are assumed to be destroyed EITHER after * they have been extracted from a list OR by the CLIST destructor which @@ -50,13 +50,13 @@ class DLLSYM CLIST_LINK data = next = NULL; } - CLIST_LINK( //copy constructor - const CLIST_LINK &) { //don't copy link + CLIST_LINK( // copy constructor + const CLIST_LINK &) { // don't copy link data = next = NULL; } - void operator= ( //don't copy links - const CLIST_LINK &) { + void operator=( // don't copy links + const CLIST_LINK &) { data = next = NULL; } }; @@ -89,8 +89,8 @@ class DLLSYM CLIST void internal_deep_clear ( //destroy all links void (*zapper) (void *)); //ptr to zapper functn - void shallow_clear(); //clear list but don't - //delete data elements + void shallow_clear(); // clear list but don't + // delete data elements bool empty() const { //is list empty? return !last; @@ -136,9 +136,10 @@ class DLLSYM CLIST }; /*********************************************************************** - * CLASS - CLIST_ITERATOR + * CLASS - CLIST_ITERATOR * - * Generic iterator class for singly linked lists with embedded links + * Generic iterator class for singly linked lists with embedded + *links **********************************************************************/ class DLLSYM CLIST_ITERATOR @@ -231,8 +232,8 @@ class DLLSYM CLIST_ITERATOR BOOL8 cycled_list(); //Completed a cycle? - void add_to_end( //add at end & - void *new_data); //don't move + void add_to_end( // add at end & + void *new_data); // don't move void exchange( //positions of 2 links CLIST_ITERATOR *other_it); //other iterator @@ -246,7 +247,7 @@ class DLLSYM CLIST_ITERATOR }; /*********************************************************************** - * CLIST_ITERATOR::set_to_list + * CLIST_ITERATOR::set_to_list * * (Re-)initialise the iterator to point to the start of the list_to_iterate * over. @@ -270,9 +271,8 @@ inline void CLIST_ITERATOR::set_to_list( //change list ex_current_was_cycle_pt = FALSE; } - /*********************************************************************** - * CLIST_ITERATOR::CLIST_ITERATOR + * CLIST_ITERATOR::CLIST_ITERATOR * * CONSTRUCTOR - set iterator to specified list; **********************************************************************/ @@ -281,9 +281,8 @@ inline CLIST_ITERATOR::CLIST_ITERATOR(CLIST *list_to_iterate) { set_to_list(list_to_iterate); } - /*********************************************************************** - * CLIST_ITERATOR::add_after_then_move + * CLIST_ITERATOR::add_after_then_move * * Add a new element to the list after the current element and move the * iterator to the new element. @@ -329,9 +328,8 @@ inline void CLIST_ITERATOR::add_after_then_move( // element to add current = new_element; } - /*********************************************************************** - * CLIST_ITERATOR::add_after_stay_put + * CLIST_ITERATOR::add_after_stay_put * * Add a new element to the list after the current element but do not move * the iterator to the new element. @@ -380,9 +378,8 @@ inline void CLIST_ITERATOR::add_after_stay_put( // element to add } } - /*********************************************************************** - * CLIST_ITERATOR::add_before_then_move + * CLIST_ITERATOR::add_before_then_move * * Add a new element to the list before the current element and move the * iterator to the new element. @@ -425,9 +422,8 @@ inline void CLIST_ITERATOR::add_before_then_move( // element to add current = new_element; } - /*********************************************************************** - * CLIST_ITERATOR::add_before_stay_put + * CLIST_ITERATOR::add_before_stay_put * * Add a new element to the list before the current element but don't move the * iterator to the new element. @@ -471,11 +467,11 @@ inline void CLIST_ITERATOR::add_before_stay_put( // element to add } } - /*********************************************************************** - * CLIST_ITERATOR::add_list_after + * CLIST_ITERATOR::add_list_after * - * Insert another list to this list after the current element but don't move the + * Insert another list to this list after the current element but don't move + *the * iterator. **********************************************************************/ @@ -518,9 +514,8 @@ inline void CLIST_ITERATOR::add_list_after(CLIST *list_to_add) { } } - /*********************************************************************** - * CLIST_ITERATOR::add_list_before + * CLIST_ITERATOR::add_list_before * * Insert another list to this list before the current element. Move the * iterator to the start of the inserted elements @@ -563,9 +558,8 @@ inline void CLIST_ITERATOR::add_list_before(CLIST *list_to_add) { } } - /*********************************************************************** - * CLIST_ITERATOR::extract + * CLIST_ITERATOR::extract * * Do extraction by removing current from the list, deleting the cons cell * and returning the data to the caller, but NOT updating the iterator. (So @@ -606,9 +600,8 @@ inline void *CLIST_ITERATOR::extract() { return extracted_data; } - /*********************************************************************** - * CLIST_ITERATOR::move_to_first() + * CLIST_ITERATOR::move_to_first() * * Move current so that it is set to the start of the list. * Return data just in case anyone wants it. @@ -626,9 +619,8 @@ inline void *CLIST_ITERATOR::move_to_first() { return current != NULL ? current->data : NULL; } - /*********************************************************************** - * CLIST_ITERATOR::mark_cycle_pt() + * CLIST_ITERATOR::mark_cycle_pt() * * Remember the current location so that we can tell whether we've returned * to this point later. @@ -651,9 +643,8 @@ inline void CLIST_ITERATOR::mark_cycle_pt() { started_cycling = FALSE; } - /*********************************************************************** - * CLIST_ITERATOR::at_first() + * CLIST_ITERATOR::at_first() * * Are we at the start of the list? * @@ -671,9 +662,8 @@ inline BOOL8 CLIST_ITERATOR::at_first() { !ex_current_was_last)); //first and last } - /*********************************************************************** - * CLIST_ITERATOR::at_last() + * CLIST_ITERATOR::at_last() * * Are we at the end of the list? * @@ -691,9 +681,8 @@ inline BOOL8 CLIST_ITERATOR::at_last() { ex_current_was_last)); //first and last } - /*********************************************************************** - * CLIST_ITERATOR::cycled_list() + * CLIST_ITERATOR::cycled_list() * * Have we returned to the cycle_pt since it was set? * @@ -709,9 +698,8 @@ inline BOOL8 CLIST_ITERATOR::cycled_list() { } - /*********************************************************************** - * CLIST_ITERATOR::length() + * CLIST_ITERATOR::length() * * Return the length of the list * @@ -726,9 +714,8 @@ inline inT32 CLIST_ITERATOR::length() { return list->length (); } - /*********************************************************************** - * CLIST_ITERATOR::sort() + * CLIST_ITERATOR::sort() * * Sort the elements of the list, then reposition at the start. * @@ -747,9 +734,8 @@ const void *, const void *)) { move_to_first(); } - /*********************************************************************** - * CLIST_ITERATOR::add_to_end + * CLIST_ITERATOR::add_to_end * * Add a new element to the end of the list without moving the iterator. * This is provided because a single linked list cannot move to the last as @@ -811,7 +797,7 @@ The macro generates: - An element deletion function: CLASSNAME##_c1_zapper - An element copier function: CLASSNAME##_c1_copier - - A CLIST subclass: CLASSNAME##_CLIST + - A CLIST subclass: CLASSNAME##_CLIST - A CLIST_ITERATOR subclass: CLASSNAME##_C_IT @@ -830,114 +816,116 @@ CLISTIZEH is a concatenation of 3 fragments CLISTIZEH_A, CLISTIZEH_B and CLISTIZEH_C. ***********************************************************************/ -#define CLISTIZEH_A( CLASSNAME ) \ - \ -extern DLLSYM void CLASSNAME##_c1_zapper( /*delete a link*/ \ -void* link); /*link to delete*/ \ - \ -extern DLLSYM void* CLASSNAME##_c1_copier( /*deep copy a link*/ \ -void* old_element); /*source link */ +#define CLISTIZEH_A(CLASSNAME) \ + \ + extern DLLSYM void CLASSNAME##_c1_zapper( /*delete a link*/ \ + void *link); /*link to delete*/ \ + \ + extern DLLSYM void \ + *CLASSNAME##_c1_copier( /*deep copy a link*/ \ + void *old_element); /*source link */ -#define CLISTIZEH_B( CLASSNAME ) \ - \ -/*********************************************************************** \ -* CLASS - CLASSNAME##_CLIST \ -* \ -* List class for class CLASSNAME \ -* \ -**********************************************************************/ \ - \ -class DLLSYM CLASSNAME##_CLIST : public CLIST \ -{ \ -public: \ - CLASSNAME##_CLIST():CLIST() {} \ - /* constructor */ \ - \ - CLASSNAME##_CLIST( /* don't construct */ \ - const CLASSNAME##_CLIST&) /*by initial assign*/ \ - { DONT_CONSTRUCT_LIST_BY_COPY.error( QUOTE_IT( CLASSNAME##_CLIST ), \ - ABORT, NULL ); } \ - \ -void deep_clear() /* delete elements */ \ - { CLIST::internal_deep_clear( &CLASSNAME##_c1_zapper ); } \ - \ -void operator=( /* prevent assign */ \ - const CLASSNAME##_CLIST&) \ - { DONT_ASSIGN_LISTS.error( QUOTE_IT( CLASSNAME##_CLIST ), \ - ABORT, NULL ); } +#define CLISTIZEH_B(CLASSNAME) \ + \ + /*********************************************************************** \ + * CLASS - \ + *CLASSNAME##_CLIST \ + * \ + * List class for class \ + *CLASSNAME \ + * \ + **********************************************************************/ \ + \ + class DLLSYM CLASSNAME##_CLIST : public CLIST { \ + public: \ + CLASSNAME##_CLIST() : CLIST() {} \ + /* constructor */ \ + \ + CLASSNAME##_CLIST( /* don't construct */ \ + const CLASSNAME##_CLIST &) /*by initial assign*/ \ + { \ + DONT_CONSTRUCT_LIST_BY_COPY.error(QUOTE_IT(CLASSNAME##_CLIST), ABORT, \ + NULL); \ + } \ + \ + void deep_clear() /* delete elements */ \ + { \ + CLIST::internal_deep_clear(&CLASSNAME##_c1_zapper); \ + } \ + \ + void operator=(/* prevent assign */ \ + const CLASSNAME##_CLIST &) { \ + DONT_ASSIGN_LISTS.error(QUOTE_IT(CLASSNAME##_CLIST), ABORT, NULL); \ + } -#define CLISTIZEH_C( CLASSNAME ) \ - \ -}; \ - \ - \ - \ -/*********************************************************************** \ -* CLASS - CLASSNAME##_C_IT \ -* \ -* Iterator class for class CLASSNAME##_CLIST \ -* \ -* Note: We don't need to coerce pointers to member functions input \ -* parameters as these are automatically converted to the type of the base \ -* type. ("A ptr to a class may be converted to a pointer to a public base \ -* class of that class") \ -**********************************************************************/ \ - \ -class DLLSYM CLASSNAME##_C_IT : public CLIST_ITERATOR \ -{ \ -public: \ - CLASSNAME##_C_IT():CLIST_ITERATOR(){} \ - \ - CLASSNAME##_C_IT( \ - CLASSNAME##_CLIST* list):CLIST_ITERATOR(list){} \ - \ - CLASSNAME* data() \ - { return (CLASSNAME*) CLIST_ITERATOR::data(); } \ - \ - CLASSNAME* data_relative( \ - inT8 offset) \ - { return (CLASSNAME*) CLIST_ITERATOR::data_relative( offset ); } \ - \ - CLASSNAME* forward() \ - { return (CLASSNAME*) CLIST_ITERATOR::forward(); } \ - \ - CLASSNAME* extract() \ - { return (CLASSNAME*) CLIST_ITERATOR::extract(); } \ - \ - CLASSNAME* move_to_first() \ - { return (CLASSNAME*) CLIST_ITERATOR::move_to_first(); } \ - \ - CLASSNAME* move_to_last() \ - { return (CLASSNAME*) CLIST_ITERATOR::move_to_last(); } \ -}; +#define CLISTIZEH_C(CLASSNAME) \ + } \ + ; \ + \ + /*********************************************************************** \ + * CLASS - CLASSNAME##_C_IT \ + * \ + * Iterator class for class CLASSNAME##_CLIST \ + * \ + * Note: We don't need to coerce pointers to member functions input \ + * parameters as these are automatically converted to the type of the base \ + * type. ("A ptr to a class may be converted to a pointer to a public base \ + * class of that class") \ + **********************************************************************/ \ + \ + class DLLSYM CLASSNAME##_C_IT : public CLIST_ITERATOR { \ + public: \ + CLASSNAME##_C_IT() : CLIST_ITERATOR() {} \ + \ + CLASSNAME##_C_IT(CLASSNAME##_CLIST *list) : CLIST_ITERATOR(list) {} \ + \ + CLASSNAME *data() { return (CLASSNAME *)CLIST_ITERATOR::data(); } \ + \ + CLASSNAME *data_relative(inT8 offset) { \ + return (CLASSNAME *)CLIST_ITERATOR::data_relative(offset); \ + } \ + \ + CLASSNAME *forward() { return (CLASSNAME *)CLIST_ITERATOR::forward(); } \ + \ + CLASSNAME *extract() { return (CLASSNAME *)CLIST_ITERATOR::extract(); } \ + \ + CLASSNAME *move_to_first() { \ + return (CLASSNAME *)CLIST_ITERATOR::move_to_first(); \ + } \ + \ + CLASSNAME *move_to_last() { \ + return (CLASSNAME *)CLIST_ITERATOR::move_to_last(); \ + } \ + }; -#define CLISTIZEH( CLASSNAME ) \ - \ -CLISTIZEH_A( CLASSNAME ) \ - \ -CLISTIZEH_B( CLASSNAME ) \ - \ -CLISTIZEH_C( CLASSNAME ) +#define CLISTIZEH(CLASSNAME) \ + \ + CLISTIZEH_A(CLASSNAME) \ + \ + CLISTIZEH_B(CLASSNAME) \ + \ + CLISTIZEH_C(CLASSNAME) /*********************************************************************** CLISTIZE( CLASSNAME ) MACRO ***********************************************************************/ -#define CLISTIZE( CLASSNAME ) \ - \ -/*********************************************************************** \ -* CLASSNAME##_c1_zapper \ -* \ -* A function which can delete a CLASSNAME element. This is passed to the \ -* generic deep_clear list member function so that when a list is cleared the \ -* elements on the list are properly destroyed from the base class, even \ -* though we don't use a virtual destructor function. \ -**********************************************************************/ \ - \ -DLLSYM void CLASSNAME##_c1_zapper( /*delete a link*/ \ -void* link) /*link to delete*/ \ -{ \ -delete (CLASSNAME *) link; \ -} \ +#define CLISTIZE(CLASSNAME) \ + \ + /*********************************************************************** \ + * CLASSNAME##_c1_zapper \ + * \ + * A function which can delete a CLASSNAME element. This is passed to the \ + * generic deep_clear list member function so that when a list is cleared \ + *the \ + * elements on the list are properly destroyed from the base class, even \ + * though we don't use a virtual destructor function. \ + **********************************************************************/ \ + \ + DLLSYM void CLASSNAME##_c1_zapper( /*delete a link*/ \ + void *link) /*link to delete*/ \ + { \ + delete (CLASSNAME *)link; \ + } #endif diff --git a/ccutil/elst.cpp b/ccutil/elst.cpp index 8ad999b5b..2d2c9ad65 100644 --- a/ccutil/elst.cpp +++ b/ccutil/elst.cpp @@ -26,7 +26,7 @@ **********************************************************************/ /*********************************************************************** - * ELIST::internal_clear + * ELIST::internal_clear * * Used by the destructor and the "clear" member function of derived list * classes to destroy all the elements on the list. @@ -57,7 +57,7 @@ void (*zapper) (ELIST_LINK *)) { } /*********************************************************************** - * ELIST::assign_to_sublist + * ELIST::assign_to_sublist * * The list is set to a sublist of another list. "This" list must be empty * before this function is invoked. The two iterators passed must refer to @@ -81,9 +81,8 @@ void ELIST::assign_to_sublist( //to this list last = start_it->extract_sublist (end_it); } - /*********************************************************************** - * ELIST::length + * ELIST::length * * Return count of elements on list **********************************************************************/ @@ -97,9 +96,8 @@ inT32 ELIST::length() const { // count elements return count; } - /*********************************************************************** - * ELIST::sort + * ELIST::sort * * Sort elements on list * NB If you don't like the const declarations in the comparator, coerce yours: @@ -187,7 +185,7 @@ ELIST_LINK *ELIST::add_sorted_and_find( **********************************************************************/ /*********************************************************************** - * ELIST_ITERATOR::forward + * ELIST_ITERATOR::forward * * Move the iterator to the next element of the list. * REMEMBER: ALL LISTS ARE CIRCULAR. @@ -224,9 +222,8 @@ ELIST_LINK *ELIST_ITERATOR::forward() { return current; } - /*********************************************************************** - * ELIST_ITERATOR::data_relative + * ELIST_ITERATOR::data_relative * * Return the data pointer to the element "offset" elements from current. * "offset" must not be less than -1. @@ -260,9 +257,8 @@ ELIST_LINK *ELIST_ITERATOR::data_relative( //get data + or - ... return ptr; } - /*********************************************************************** - * ELIST_ITERATOR::move_to_last() + * ELIST_ITERATOR::move_to_last() * * Move current so that it is set to the end of the list. * Return data just in case anyone wants it. @@ -281,9 +277,8 @@ ELIST_LINK *ELIST_ITERATOR::move_to_last() { return current; } - /*********************************************************************** - * ELIST_ITERATOR::exchange() + * ELIST_ITERATOR::exchange() * * Given another iterator, whose current element is a different element on * the same list list OR an element of another list, exchange the two current @@ -379,9 +374,8 @@ void ELIST_ITERATOR::exchange( //positions of 2 link other_it->current = old_current; } - /*********************************************************************** - * ELIST_ITERATOR::extract_sublist() + * ELIST_ITERATOR::extract_sublist() * * This is a private member, used only by ELIST::assign_to_sublist. * Given another iterator for the same list, extract the links from THIS to @@ -425,7 +419,7 @@ ELIST_LINK *ELIST_ITERATOR::extract_sublist( //from temp_it.mark_cycle_pt (); do { //walk sublist - if (temp_it.cycled_list ()) //can't find end pt + if (temp_it.cycled_list()) // can't find end pt BAD_SUBLIST.error ("ELIST_ITERATOR.extract_sublist", ABORT, NULL); if (temp_it.at_last ()) { diff --git a/ccutil/elst.h b/ccutil/elst.h index e23957741..d53a7c34f 100644 --- a/ccutil/elst.h +++ b/ccutil/elst.h @@ -98,8 +98,8 @@ class DLLSYM ELIST_LINK next = NULL; } - void operator= ( //don't copy links - const ELIST_LINK &) { + void operator=( // don't copy links + const ELIST_LINK &) { next = NULL; } }; @@ -273,8 +273,8 @@ class DLLSYM ELIST_ITERATOR bool cycled_list(); //Completed a cycle? - void add_to_end( //add at end & - ELIST_LINK *new_link); //don't move + void add_to_end( // add at end & + ELIST_LINK *new_link); // don't move void exchange( //positions of 2 links ELIST_ITERATOR *other_it); //other iterator @@ -458,7 +458,6 @@ inline void ELIST_ITERATOR::add_before_then_move( // element to add current = new_element; } - /*********************************************************************** * ELIST_ITERATOR::add_before_stay_put * @@ -501,11 +500,11 @@ inline void ELIST_ITERATOR::add_before_stay_put( // element to add } } - /*********************************************************************** * ELIST_ITERATOR::add_list_after * - * Insert another list to this list after the current element but don't move the + * Insert another list to this list after the current element but don't move + *the * iterator. **********************************************************************/ @@ -959,30 +958,29 @@ ELISTIZEH_C( CLASSNAME ) ELISTIZE( CLASSNAME ) MACRO ***********************************************************************/ -#define ELISTIZE(CLASSNAME) \ - \ -/*********************************************************************** \ -* CLASSNAME##_zapper \ -* \ -* A function which can delete a CLASSNAME element. This is passed to the \ -* generic clear list member function so that when a list is cleared the \ -* elements on the list are properly destroyed from the base class, even \ -* though we don't use a virtual destructor function. \ -**********************************************************************/ \ - \ -DLLSYM void CLASSNAME##_zapper(ELIST_LINK* link) { \ - delete reinterpret_cast(link); \ -} \ - \ -/* Become a deep copy of src_list*/ \ -void CLASSNAME##_LIST::deep_copy(const CLASSNAME##_LIST* src_list, \ - CLASSNAME* (*copier)(const CLASSNAME*)) { \ - \ - CLASSNAME##_IT from_it(const_cast(src_list)); \ - CLASSNAME##_IT to_it(this); \ - \ - for (from_it.mark_cycle_pt(); !from_it.cycled_list(); from_it.forward()) \ - to_it.add_after_then_move((*copier)(from_it.data())); \ -} +#define ELISTIZE(CLASSNAME) \ + \ + /*********************************************************************** \ + * CLASSNAME##_zapper \ + * \ + * A function which can delete a CLASSNAME element. This is passed to the \ + * generic clear list member function so that when a list is cleared the \ + * elements on the list are properly destroyed from the base class, even \ + * though we don't use a virtual destructor function. \ + **********************************************************************/ \ + \ + DLLSYM void CLASSNAME##_zapper(ELIST_LINK *link) { \ + delete reinterpret_cast(link); \ + } \ + \ + /* Become a deep copy of src_list*/ \ + void CLASSNAME##_LIST::deep_copy(const CLASSNAME##_LIST *src_list, \ + CLASSNAME *(*copier)(const CLASSNAME *)) { \ + CLASSNAME##_IT from_it(const_cast(src_list)); \ + CLASSNAME##_IT to_it(this); \ + \ + for (from_it.mark_cycle_pt(); !from_it.cycled_list(); from_it.forward()) \ + to_it.add_after_then_move((*copier)(from_it.data())); \ + } #endif diff --git a/ccutil/elst2.cpp b/ccutil/elst2.cpp index 30cedec17..0d4960ed4 100644 --- a/ccutil/elst2.cpp +++ b/ccutil/elst2.cpp @@ -27,7 +27,7 @@ **********************************************************************/ /*********************************************************************** - * ELIST2::internal_clear + * ELIST2::internal_clear * * Used by the destructor and the "clear" member function of derived list * classes to destroy all the elements on the list. @@ -58,7 +58,7 @@ void (*zapper) (ELIST2_LINK *)) { } /*********************************************************************** - * ELIST2::assign_to_sublist + * ELIST2::assign_to_sublist * * The list is set to a sublist of another list. "This" list must be empty * before this function is invoked. The two iterators passed must refer to @@ -82,9 +82,8 @@ void ELIST2::assign_to_sublist( //to this list last = start_it->extract_sublist (end_it); } - /*********************************************************************** - * ELIST2::length + * ELIST2::length * * Return count of elements on list **********************************************************************/ @@ -98,9 +97,8 @@ inT32 ELIST2::length() const { // count elements return count; } - /*********************************************************************** - * ELIST2::sort + * ELIST2::sort * * Sort elements on list * NB If you don't like the const declarations in the comparator, coerce yours: @@ -180,7 +178,7 @@ void ELIST2::add_sorted(int comparator(const void*, const void*), **********************************************************************/ /*********************************************************************** - * ELIST2_ITERATOR::forward + * ELIST2_ITERATOR::forward * * Move the iterator to the next element of the list. * REMEMBER: ALL LISTS ARE CIRCULAR. @@ -218,9 +216,8 @@ ELIST2_LINK *ELIST2_ITERATOR::forward() { return current; } - /*********************************************************************** - * ELIST2_ITERATOR::backward + * ELIST2_ITERATOR::backward * * Move the iterator to the previous element of the list. * REMEMBER: ALL LISTS ARE CIRCULAR. @@ -257,9 +254,8 @@ ELIST2_LINK *ELIST2_ITERATOR::backward() { return current; } - /*********************************************************************** - * ELIST2_ITERATOR::data_relative + * ELIST2_ITERATOR::data_relative * * Return the data pointer to the element "offset" elements from current. * (This function can't be INLINEd because it contains a loop) @@ -289,9 +285,8 @@ ELIST2_LINK *ELIST2_ITERATOR::data_relative( //get data + or - .. return ptr; } - /*********************************************************************** - * ELIST2_ITERATOR::exchange() + * ELIST2_ITERATOR::exchange() * * Given another iterator, whose current element is a different element on * the same list list OR an element of another list, exchange the two current @@ -399,9 +394,8 @@ void ELIST2_ITERATOR::exchange( //positions of 2 li other_it->current = old_current; } - /*********************************************************************** - * ELIST2_ITERATOR::extract_sublist() + * ELIST2_ITERATOR::extract_sublist() * * This is a private member, used only by ELIST2::assign_to_sublist. * Given another iterator for the same list, extract the links from THIS to @@ -445,7 +439,7 @@ ELIST2_LINK *ELIST2_ITERATOR::extract_sublist( //fr temp_it.mark_cycle_pt (); do { //walk sublist - if (temp_it.cycled_list ()) //can't find end pt + if (temp_it.cycled_list()) // can't find end pt BAD_SUBLIST.error ("ELIST2_ITERATOR.extract_sublist", ABORT, NULL); if (temp_it.at_last ()) { diff --git a/ccutil/elst2.h b/ccutil/elst2.h index 364abd86b..bf078fbd5 100644 --- a/ccutil/elst2.h +++ b/ccutil/elst2.h @@ -46,9 +46,9 @@ i) The duplication in source does not affect the run time code size - the **********************************************************************/ /********************************************************************** - * CLASS - ELIST2_LINK + * CLASS - ELIST2_LINK * - * Generic link class for doubly linked lists with embedded links + * Generic link class for doubly linked lists with embedded links * * Note: No destructor - elements are assumed to be destroyed EITHER after * they have been extracted from a list OR by the ELIST2 destructor which @@ -68,13 +68,13 @@ class DLLSYM ELIST2_LINK prev = next = NULL; } - ELIST2_LINK( //copy constructor - const ELIST2_LINK &) { //don't copy link + ELIST2_LINK( // copy constructor + const ELIST2_LINK &) { // don't copy link prev = next = NULL; } - void operator= ( //don't copy links - const ELIST2_LINK &) { + void operator=( // don't copy links + const ELIST2_LINK &) { prev = next = NULL; } }; @@ -142,9 +142,10 @@ class DLLSYM ELIST2 }; /*********************************************************************** - * CLASS - ELIST2_ITERATOR + * CLASS - ELIST2_ITERATOR * - * Generic iterator class for doubly linked lists with embedded links + * Generic iterator class for doubly linked lists with embedded + *links **********************************************************************/ class DLLSYM ELIST2_ITERATOR @@ -240,8 +241,8 @@ class DLLSYM ELIST2_ITERATOR BOOL8 cycled_list(); //Completed a cycle? - void add_to_end( //add at end & - ELIST2_LINK *new_link); //don't move + void add_to_end( // add at end & + ELIST2_LINK *new_link); // don't move void exchange( //positions of 2 links ELIST2_ITERATOR *other_it); //other iterator @@ -255,7 +256,7 @@ class DLLSYM ELIST2_ITERATOR }; /*********************************************************************** - * ELIST2_ITERATOR::set_to_list + * ELIST2_ITERATOR::set_to_list * * (Re-)initialise the iterator to point to the start of the list_to_iterate * over. @@ -279,9 +280,8 @@ inline void ELIST2_ITERATOR::set_to_list( //change list ex_current_was_cycle_pt = FALSE; } - /*********************************************************************** - * ELIST2_ITERATOR::ELIST2_ITERATOR + * ELIST2_ITERATOR::ELIST2_ITERATOR * * CONSTRUCTOR - set iterator to specified list; **********************************************************************/ @@ -290,9 +290,8 @@ inline ELIST2_ITERATOR::ELIST2_ITERATOR(ELIST2 *list_to_iterate) { set_to_list(list_to_iterate); } - /*********************************************************************** - * ELIST2_ITERATOR::add_after_then_move + * ELIST2_ITERATOR::add_after_then_move * * Add a new element to the list after the current element and move the * iterator to the new element. @@ -339,9 +338,8 @@ inline void ELIST2_ITERATOR::add_after_then_move( // element to add current = new_element; } - /*********************************************************************** - * ELIST2_ITERATOR::add_after_stay_put + * ELIST2_ITERATOR::add_after_stay_put * * Add a new element to the list after the current element but do not move * the iterator to the new element. @@ -391,9 +389,8 @@ inline void ELIST2_ITERATOR::add_after_stay_put( // element to add } } - /*********************************************************************** - * ELIST2_ITERATOR::add_before_then_move + * ELIST2_ITERATOR::add_before_then_move * * Add a new element to the list before the current element and move the * iterator to the new element. @@ -438,9 +435,8 @@ inline void ELIST2_ITERATOR::add_before_then_move( // element to add current = new_element; } - /*********************************************************************** - * ELIST2_ITERATOR::add_before_stay_put + * ELIST2_ITERATOR::add_before_stay_put * * Add a new element to the list before the current element but don't move the * iterator to the new element. @@ -486,11 +482,11 @@ inline void ELIST2_ITERATOR::add_before_stay_put( // element to add } } - /*********************************************************************** - * ELIST2_ITERATOR::add_list_after + * ELIST2_ITERATOR::add_list_after * - * Insert another list to this list after the current element but don't move the + * Insert another list to this list after the current element but don't move + *the * iterator. **********************************************************************/ @@ -537,9 +533,8 @@ inline void ELIST2_ITERATOR::add_list_after(ELIST2 *list_to_add) { } } - /*********************************************************************** - * ELIST2_ITERATOR::add_list_before + * ELIST2_ITERATOR::add_list_before * * Insert another list to this list before the current element. Move the * iterator to the start of the inserted elements @@ -586,9 +581,8 @@ inline void ELIST2_ITERATOR::add_list_before(ELIST2 *list_to_add) { } } - /*********************************************************************** - * ELIST2_ITERATOR::extract + * ELIST2_ITERATOR::extract * * Do extraction by removing current from the list, returning it to the * caller, but NOT updating the iterator. (So that any calling loop can do @@ -631,9 +625,8 @@ inline ELIST2_LINK *ELIST2_ITERATOR::extract() { return extracted_link; } - /*********************************************************************** - * ELIST2_ITERATOR::move_to_first() + * ELIST2_ITERATOR::move_to_first() * * Move current so that it is set to the start of the list. * Return data just in case anyone wants it. @@ -651,9 +644,8 @@ inline ELIST2_LINK *ELIST2_ITERATOR::move_to_first() { return current; } - /*********************************************************************** - * ELIST2_ITERATOR::move_to_last() + * ELIST2_ITERATOR::move_to_last() * * Move current so that it is set to the end of the list. * Return data just in case anyone wants it. @@ -671,9 +663,8 @@ inline ELIST2_LINK *ELIST2_ITERATOR::move_to_last() { return current; } - /*********************************************************************** - * ELIST2_ITERATOR::mark_cycle_pt() + * ELIST2_ITERATOR::mark_cycle_pt() * * Remember the current location so that we can tell whether we've returned * to this point later. @@ -696,9 +687,8 @@ inline void ELIST2_ITERATOR::mark_cycle_pt() { started_cycling = FALSE; } - /*********************************************************************** - * ELIST2_ITERATOR::at_first() + * ELIST2_ITERATOR::at_first() * * Are we at the start of the list? * @@ -716,9 +706,8 @@ inline BOOL8 ELIST2_ITERATOR::at_first() { !ex_current_was_last)); //first and last } - /*********************************************************************** - * ELIST2_ITERATOR::at_last() + * ELIST2_ITERATOR::at_last() * * Are we at the end of the list? * @@ -736,9 +725,8 @@ inline BOOL8 ELIST2_ITERATOR::at_last() { ex_current_was_last)); //first and last } - /*********************************************************************** - * ELIST2_ITERATOR::cycled_list() + * ELIST2_ITERATOR::cycled_list() * * Have we returned to the cycle_pt since it was set? * @@ -754,9 +742,8 @@ inline BOOL8 ELIST2_ITERATOR::cycled_list() { } - /*********************************************************************** - * ELIST2_ITERATOR::length() + * ELIST2_ITERATOR::length() * * Return the length of the list * @@ -771,9 +758,8 @@ inline inT32 ELIST2_ITERATOR::length() { return list->length (); } - /*********************************************************************** - * ELIST2_ITERATOR::sort() + * ELIST2_ITERATOR::sort() * * Sort the elements of the list, then reposition at the start. * @@ -792,9 +778,8 @@ const void *, const void *)) { move_to_first(); } - /*********************************************************************** - * ELIST2_ITERATOR::add_to_end + * ELIST2_ITERATOR::add_to_end * * Add a new element to the end of the list without moving the iterator. * This is provided because a single linked list cannot move to the last as @@ -854,7 +839,7 @@ will NOT work correctly for classes derived from this. The macro generates: - An element deletion function: CLASSNAME##_zapper - - An E_LIST2 subclass: CLASSNAME##_LIST + - An E_LIST2 subclass: CLASSNAME##_LIST - An E_LIST2_ITERATOR subclass: CLASSNAME##_IT @@ -873,132 +858,132 @@ ELIST2IZEH is a concatenation of 3 fragments ELIST2IZEH_A, ELIST2IZEH_B and ELIST2IZEH_C. ***********************************************************************/ -#define ELIST2IZEH_A( CLASSNAME ) \ - \ -extern DLLSYM void CLASSNAME##_zapper( /*delete a link*/ \ -ELIST2_LINK* link); /*link to delete*/ +#define ELIST2IZEH_A(CLASSNAME) \ + \ + extern DLLSYM void CLASSNAME##_zapper( /*delete a link*/ \ + ELIST2_LINK *link); /*link to delete*/ -#define ELIST2IZEH_B( CLASSNAME ) \ - \ -/*********************************************************************** \ -* CLASS - CLASSNAME##_LIST \ -* \ -* List class for class CLASSNAME \ -* \ -**********************************************************************/ \ - \ -class DLLSYM CLASSNAME##_LIST : public ELIST2 \ -{ \ -public: \ - CLASSNAME##_LIST():ELIST2() {} \ - /* constructor */ \ - \ - CLASSNAME##_LIST( /* don't construct */ \ - const CLASSNAME##_LIST&) /*by initial assign*/\ - { DONT_CONSTRUCT_LIST_BY_COPY.error( QUOTE_IT( CLASSNAME##_LIST ), \ - ABORT, NULL ); } \ - \ -void clear() /* delete elements */\ - { ELIST2::internal_clear( &CLASSNAME##_zapper ); } \ - \ - ~CLASSNAME##_LIST() /* destructor */ \ - { clear(); } \ -\ -/* Become a deep copy of src_list*/ \ -void deep_copy(const CLASSNAME##_LIST* src_list, \ - CLASSNAME* (*copier)(const CLASSNAME*)); \ -\ -void operator=( /* prevent assign */ \ - const CLASSNAME##_LIST&) \ - { DONT_ASSIGN_LISTS.error( QUOTE_IT( CLASSNAME##_LIST ), \ - ABORT, NULL ); } +#define ELIST2IZEH_B(CLASSNAME) \ + \ + /*********************************************************************** \ + * CLASS - \ + *CLASSNAME##_LIST \ + * \ + * List class for class \ + *CLASSNAME \ + * \ + **********************************************************************/ \ + \ + class DLLSYM CLASSNAME##_LIST : public ELIST2 { \ + public: \ + CLASSNAME##_LIST() : ELIST2() {} \ + /* constructor */ \ + \ + CLASSNAME##_LIST( /* don't construct */ \ + const CLASSNAME##_LIST &) /*by initial assign*/ \ + { \ + DONT_CONSTRUCT_LIST_BY_COPY.error(QUOTE_IT(CLASSNAME##_LIST), ABORT, \ + NULL); \ + } \ + \ + void clear() /* delete elements */ \ + { \ + ELIST2::internal_clear(&CLASSNAME##_zapper); \ + } \ + \ + ~CLASSNAME##_LIST() /* destructor */ \ + { \ + clear(); \ + } \ + \ + /* Become a deep copy of src_list*/ \ + void deep_copy(const CLASSNAME##_LIST *src_list, \ + CLASSNAME *(*copier)(const CLASSNAME *)); \ + \ + void operator=(/* prevent assign */ \ + const CLASSNAME##_LIST &) { \ + DONT_ASSIGN_LISTS.error(QUOTE_IT(CLASSNAME##_LIST), ABORT, NULL); \ + } -#define ELIST2IZEH_C( CLASSNAME ) \ -}; \ - \ - \ - \ -/*********************************************************************** \ -* CLASS - CLASSNAME##_IT \ -* \ -* Iterator class for class CLASSNAME##_LIST \ -* \ -* Note: We don't need to coerce pointers to member functions input \ -* parameters as these are automatically converted to the type of the base \ -* type. ("A ptr to a class may be converted to a pointer to a public base \ -* class of that class") \ -**********************************************************************/ \ - \ -class DLLSYM CLASSNAME##_IT : public ELIST2_ITERATOR \ -{ \ -public: \ - CLASSNAME##_IT():ELIST2_ITERATOR(){} \ - \ - CLASSNAME##_IT( \ -CLASSNAME##_LIST* list):ELIST2_ITERATOR(list){} \ - \ - CLASSNAME* data() \ - { return (CLASSNAME*) ELIST2_ITERATOR::data(); } \ - \ - CLASSNAME* data_relative( \ - inT8 offset) \ - { return (CLASSNAME*) ELIST2_ITERATOR::data_relative( offset ); } \ - \ - CLASSNAME* forward() \ - { return (CLASSNAME*) ELIST2_ITERATOR::forward(); } \ - \ - CLASSNAME* backward() \ - { return (CLASSNAME*) ELIST2_ITERATOR::backward(); } \ - \ - CLASSNAME* extract() \ - { return (CLASSNAME*) ELIST2_ITERATOR::extract(); } \ - \ - CLASSNAME* move_to_first() \ - { return (CLASSNAME*) ELIST2_ITERATOR::move_to_first(); } \ - \ - CLASSNAME* move_to_last() \ - { return (CLASSNAME*) ELIST2_ITERATOR::move_to_last(); } \ -}; - -#define ELIST2IZEH( CLASSNAME ) \ - \ -ELIST2IZEH_A( CLASSNAME ) \ - \ -ELIST2IZEH_B( CLASSNAME ) \ - \ -ELIST2IZEH_C( CLASSNAME ) +#define ELIST2IZEH_C(CLASSNAME) \ + } \ + ; \ + \ + /*********************************************************************** \ + * CLASS - CLASSNAME##_IT \ + * \ + * Iterator class for class CLASSNAME##_LIST \ + * \ + * Note: We don't need to coerce pointers to member functions input \ + * parameters as these are automatically converted to the type of the base \ + * type. ("A ptr to a class may be converted to a pointer to a public base \ + * class of that class") \ + **********************************************************************/ \ + \ + class DLLSYM CLASSNAME##_IT : public ELIST2_ITERATOR { \ + public: \ + CLASSNAME##_IT() : ELIST2_ITERATOR() {} \ + \ + CLASSNAME##_IT(CLASSNAME##_LIST *list) : ELIST2_ITERATOR(list) {} \ + \ + CLASSNAME *data() { return (CLASSNAME *)ELIST2_ITERATOR::data(); } \ + \ + CLASSNAME *data_relative(inT8 offset) { \ + return (CLASSNAME *)ELIST2_ITERATOR::data_relative(offset); \ + } \ + \ + CLASSNAME *forward() { return (CLASSNAME *)ELIST2_ITERATOR::forward(); } \ + \ + CLASSNAME *backward() { return (CLASSNAME *)ELIST2_ITERATOR::backward(); } \ + \ + CLASSNAME *extract() { return (CLASSNAME *)ELIST2_ITERATOR::extract(); } \ + \ + CLASSNAME *move_to_first() { \ + return (CLASSNAME *)ELIST2_ITERATOR::move_to_first(); \ + } \ + \ + CLASSNAME *move_to_last() { \ + return (CLASSNAME *)ELIST2_ITERATOR::move_to_last(); \ + } \ + }; +#define ELIST2IZEH(CLASSNAME) \ + \ + ELIST2IZEH_A(CLASSNAME) \ + \ + ELIST2IZEH_B(CLASSNAME) \ + \ + ELIST2IZEH_C(CLASSNAME) /*********************************************************************** ELIST2IZE( CLASSNAME ) MACRO ***********************************************************************/ -#define ELIST2IZE( CLASSNAME ) \ - \ -/*********************************************************************** \ -* CLASSNAME##_zapper \ -* \ -* A function which can delete a CLASSNAME element. This is passed to the \ -* generic clear list member function so that when a list is cleared the \ -* elements on the list are properly destroyed from the base class, even \ -* though we don't use a virtual destructor function. \ -**********************************************************************/ \ - \ -DLLSYM void CLASSNAME##_zapper( /*delete a link*/ \ -ELIST2_LINK* link) /*link to delete*/ \ -{ \ -delete (CLASSNAME *) link; \ -} \ -\ -/* Become a deep copy of src_list*/ \ -void CLASSNAME##_LIST::deep_copy(const CLASSNAME##_LIST* src_list, \ - CLASSNAME* (*copier)(const CLASSNAME*)) { \ -\ - CLASSNAME##_IT from_it(const_cast(src_list)); \ - CLASSNAME##_IT to_it(this); \ -\ - for (from_it.mark_cycle_pt(); !from_it.cycled_list(); from_it.forward()) \ - to_it.add_after_then_move((*copier)(from_it.data())); \ -} +#define ELIST2IZE(CLASSNAME) \ + \ + /*********************************************************************** \ + * CLASSNAME##_zapper \ + * \ + * A function which can delete a CLASSNAME element. This is passed to the \ + * generic clear list member function so that when a list is cleared the \ + * elements on the list are properly destroyed from the base class, even \ + * though we don't use a virtual destructor function. \ + **********************************************************************/ \ + \ + DLLSYM void CLASSNAME##_zapper( /*delete a link*/ \ + ELIST2_LINK *link) /*link to delete*/ \ + { \ + delete (CLASSNAME *)link; \ + } \ + \ + /* Become a deep copy of src_list*/ \ + void CLASSNAME##_LIST::deep_copy(const CLASSNAME##_LIST *src_list, \ + CLASSNAME *(*copier)(const CLASSNAME *)) { \ + CLASSNAME##_IT from_it(const_cast(src_list)); \ + CLASSNAME##_IT to_it(this); \ + \ + for (from_it.mark_cycle_pt(); !from_it.cycled_list(); from_it.forward()) \ + to_it.add_after_then_move((*copier)(from_it.data())); \ + } #endif diff --git a/ccutil/errcode.h b/ccutil/errcode.h index d69024003..2f31a7b9a 100644 --- a/ccutil/errcode.h +++ b/ccutil/errcode.h @@ -87,11 +87,10 @@ const ERRCODE ASSERT_FAILED = "Assert failed"; __FILE__, __LINE__); \ } -#define ASSERT_HOST_MSG(x, ...) if (!(x)) \ - { \ - tprintf(__VA_ARGS__); \ - ASSERT_FAILED.error(#x, ABORT, "in file %s, line %d", \ - __FILE__, __LINE__); \ +#define ASSERT_HOST_MSG(x, ...) \ + if (!(x)) { \ + tprintf(__VA_ARGS__); \ + ASSERT_FAILED.error(#x, ABORT, "in file %s, line %d", __FILE__, __LINE__); \ } void signal_exit(int signal_code); diff --git a/ccutil/helpers.h b/ccutil/helpers.h index 51dd3b0b4..a2276bc45 100644 --- a/ccutil/helpers.h +++ b/ccutil/helpers.h @@ -73,7 +73,7 @@ class TRand { // Remove newline (if any) at the end of the string. inline void chomp_string(char *str) { - int last_index = (int)strlen(str) - 1; + int last_index = static_cast(strlen(str)) - 1; while (last_index >= 0 && (str[last_index] == '\n' || str[last_index] == '\r')) { str[last_index--] = '\0'; diff --git a/ccutil/host.h b/ccutil/host.h index 3ec3bcb79..2e3f1dd77 100644 --- a/ccutil/host.h +++ b/ccutil/host.h @@ -59,6 +59,7 @@ ** of the computer and/or operating system. ******************************************************************************/ +#include #include "platform.h" /* _WIN32 */ #ifdef _WIN32 @@ -121,15 +122,16 @@ typedef unsigned char BOOL8; #define MAX_UINT8 0xff #define MAX_UINT16 0xffff #define MAX_UINT32 0xffffffff -#define MAX_FLOAT32 ((float)3.40282347e+38) +#define MAX_FLOAT32 std::numeric_limits::max() -#define MIN_INT8 0x80 -#define MIN_INT16 0x8000 -#define MIN_INT32 static_cast(0x80000000) +#define MIN_INT8 static_cast(0x80) +#define MIN_INT16 static_cast(0x8000) +#define MIN_INT32 static_cast(0x80000000) #define MIN_UINT8 0x00 #define MIN_UINT16 0x0000 #define MIN_UINT32 0x00000000 -#define MIN_FLOAT32 ((float)1.17549435e-38) +// Minimum positive value ie 1e-37ish. +#define MIN_FLOAT32 std::numeric_limits::min() // Defines #ifndef TRUE diff --git a/ccutil/lsterr.h b/ccutil/lsterr.h index 42ed07e32..82497ae80 100644 --- a/ccutil/lsterr.h +++ b/ccutil/lsterr.h @@ -38,6 +38,6 @@ const ERRCODE NULL_PREV = "Previous element on the list is NULL"; const ERRCODE EMPTY_LIST = "List is empty"; const ERRCODE BAD_PARAMETER = "List parameter error"; const ERRCODE STILL_LINKED = -"Attempting to add an element with non NULL links, to a list"; + "Attempting to add an element with non NULL links, to a list"; #endif #endif diff --git a/ccutil/mainblk.cpp b/ccutil/mainblk.cpp index aa73c5500..36becce9e 100644 --- a/ccutil/mainblk.cpp +++ b/ccutil/mainblk.cpp @@ -74,7 +74,7 @@ void CCUtil::main_setup(const char *argv0, const char *basename) { #endif /* _WIN32 */ #if defined(TESSDATA_PREFIX) } else { - /* Use tessdata prefix which was compiled in. */ +/* Use tessdata prefix which was compiled in. */ #define _STR(a) #a #define _XSTR(a) _STR(a) datadir = _XSTR(TESSDATA_PREFIX); diff --git a/ccutil/ocrclass.h b/ccutil/ocrclass.h index 3175a6d29..f352956fa 100644 --- a/ccutil/ocrclass.h +++ b/ccutil/ocrclass.h @@ -1,7 +1,7 @@ /********************************************************************** * File: ocrclass.h * Description: Class definitions and constants for the OCR API. - * Author: Hewlett-Packard Co + * Author: Hewlett-Packard Co * * (C) Copyright 1996, Hewlett-Packard Co. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -110,28 +110,35 @@ typedef struct { /*single character */ * user words found. If it returns true then operation is cancelled. **********************************************************************/ typedef bool (*CANCEL_FUNC)(void* cancel_this, int words); -typedef bool (*PROGRESS_FUNC)(int progress, - int left, int right, int top, int bottom); +typedef bool (*PROGRESS_FUNC)(int progress, int left, int right, int top, + int bottom); class ETEXT_DESC { // output header public: - inT16 count; /// chars in this buffer(0) - inT16 progress; /// percent complete increasing (0-100) + inT16 count; /// chars in this buffer(0) + inT16 progress; /// percent complete increasing (0-100) /** Progress monitor covers word recognition and it does not cover layout * analysis. * See Ray comment in https://github.com/tesseract-ocr/tesseract/pull/27 */ - inT8 more_to_come; /// true if not last - volatile inT8 ocr_alive; /// ocr sets to 1, HP 0 - inT8 err_code; /// for errcode use - CANCEL_FUNC cancel; /// returns true to cancel - PROGRESS_FUNC progress_callback; /// called whenever progress increases - void* cancel_this; /// this or other data for cancel - struct timeval end_time; /** time to stop. expected to be set only by call - * to set_deadline_msecs() */ - EANYCODE_CHAR text[1]; /// character data + inT8 more_to_come; /// true if not last + volatile inT8 ocr_alive; /// ocr sets to 1, HP 0 + inT8 err_code; /// for errcode use + CANCEL_FUNC cancel; /// returns true to cancel + PROGRESS_FUNC progress_callback; /// called whenever progress increases + void* cancel_this; /// this or other data for cancel + struct timeval end_time; /// Time to stop. Expected to be set only + /// by call to set_deadline_msecs(). + EANYCODE_CHAR text[1]; /// character data - ETEXT_DESC() : count(0), progress(0), more_to_come(0), ocr_alive(0), - err_code(0), cancel(NULL), cancel_this(NULL) { + ETEXT_DESC() + : count(0), + progress(0), + more_to_come(0), + ocr_alive(0), + err_code(0), + cancel(NULL), + progress_callback(NULL), + cancel_this(NULL) { end_time.tv_sec = 0; end_time.tv_usec = 0; } diff --git a/ccutil/params.cpp b/ccutil/params.cpp index 9b604fbef..c8dd3514e 100644 --- a/ccutil/params.cpp +++ b/ccutil/params.cpp @@ -31,8 +31,7 @@ #define EQUAL '=' tesseract::ParamsVectors *GlobalParams() { - static tesseract::ParamsVectors global_params = - tesseract::ParamsVectors(); + static tesseract::ParamsVectors global_params = tesseract::ParamsVectors(); return &global_params; } diff --git a/ccutil/strngs.cpp b/ccutil/strngs.cpp index ff3bbac28..0760852e9 100644 --- a/ccutil/strngs.cpp +++ b/ccutil/strngs.cpp @@ -1,8 +1,8 @@ /********************************************************************** * File: strngs.c (Formerly strings.c) * Description: STRING class functions. - * Author: Ray Smith - * Created: Fri Feb 15 09:13:30 GMT 1991 + * Author: Ray Smith + * Created: Fri Feb 15 09:13:30 GMT 1991 * * (C) Copyright 1991, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/ccutil/unicharset.h b/ccutil/unicharset.h index 684655aff..023e84d5b 100644 --- a/ccutil/unicharset.h +++ b/ccutil/unicharset.h @@ -181,8 +181,7 @@ class UNICHARSET { // Return the UNICHAR_ID of a given unichar representation within the // UNICHARSET. Only the first length characters from unichar_repr are used. - UNICHAR_ID unichar_to_id(const char* const unichar_repr, - int length) const; + UNICHAR_ID unichar_to_id(const char* const unichar_repr, int length) const; // Return the minimum number of bytes that matches a legal UNICHAR_ID, // while leaving the rest of the string encodable. Returns 0 if the diff --git a/classify/adaptmatch.cpp b/classify/adaptmatch.cpp index 6e995c215..5ea8a49ce 100644 --- a/classify/adaptmatch.cpp +++ b/classify/adaptmatch.cpp @@ -908,8 +908,7 @@ void Classify::AdaptToChar(TBLOB* Blob, CLASS_ID ClassId, int FontinfoId, NumFeatures = GetAdaptiveFeatures(Blob, IntFeatures, &FloatFeatures); if (NumFeatures <= 0) { - FreeFeatureSet(FloatFeatures); - return; + return; // Features already freed by GetAdaptiveFeatures. } // Only match configs with the matching font. @@ -1008,8 +1007,6 @@ void Classify::DisplayAdaptedChar(TBLOB* blob, INT_CLASS_STRUCT* int_class) { #endif } - - /** * This routine adds the result of a classification into * Results. If the new rating is much worse than the current diff --git a/classify/classify.cpp b/classify/classify.cpp index 436efd1f2..7c11c51f6 100644 --- a/classify/classify.cpp +++ b/classify/classify.cpp @@ -151,8 +151,8 @@ Classify::Classify() INT_MEMBER(classify_integer_matcher_multiplier, 10, "Integer Matcher Multiplier 0-255: ", this->params()), EnableLearning(true), - INT_MEMBER(il1_adaption_test, 0, "Don't adapt to i/I at beginning of word", - this->params()), + INT_MEMBER(il1_adaption_test, 0, + "Don't adapt to i/I at beginning of word", this->params()), BOOL_MEMBER(classify_bln_numeric_mode, 0, "Assume the input is numbers [0-9].", this->params()), double_MEMBER(speckle_large_max_size, 0.30, "Max large speckle size", diff --git a/classify/cluster.cpp b/classify/cluster.cpp index b723bfa82..1f8234955 100644 --- a/classify/cluster.cpp +++ b/classify/cluster.cpp @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: cluster.c - ** Purpose: Routines for clustering points in N-D space - ** Author: Dan Johnson - ** History: 5/29/89, DSJ, Created. + ** Filename: cluster.c + ** Purpose: Routines for clustering points in N-D space + ** Author: Dan Johnson + ** History: 5/29/89, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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 @@ -390,11 +390,11 @@ double InvertMatrix(const float* input, int size, float* inv); * This routine creates a new clusterer data structure, * initializes it, and returns a pointer to it. * - * @param SampleSize number of dimensions in feature space - * @param ParamDesc description of each dimension - * @return pointer to the new clusterer data structure - * @note Exceptions: None - * @note History: 5/29/89, DSJ, Created. + * @param SampleSize number of dimensions in feature space + * @param ParamDesc description of each dimension + * @return pointer to the new clusterer data structure + * @note Exceptions: None + * @note History: 5/29/89, DSJ, Created. */ CLUSTERER * MakeClusterer (inT16 SampleSize, const PARAM_DESC ParamDesc[]) { @@ -437,7 +437,6 @@ MakeClusterer (inT16 SampleSize, const PARAM_DESC ParamDesc[]) { return Clusterer; } // MakeClusterer - /** * This routine creates a new sample data structure to hold * the specified feature. This sample is added to the clusterer @@ -445,14 +444,14 @@ MakeClusterer (inT16 SampleSize, const PARAM_DESC ParamDesc[]) { * clustered later), and a pointer to the sample is returned to * the caller. * - * @param Clusterer clusterer data structure to add sample to - * @param Feature feature to be added to clusterer - * @param CharID unique ident. of char that sample came from + * @param Clusterer clusterer data structure to add sample to + * @param Feature feature to be added to clusterer + * @param CharID unique ident. of char that sample came from * - * @return Pointer to the new sample data structure - * @note Exceptions: ALREADYCLUSTERED MakeSample can't be called after + * @return Pointer to the new sample data structure + * @note Exceptions: ALREADYCLUSTERED MakeSample can't be called after * ClusterSamples has been called - * @note History: 5/29/89, DSJ, Created. + * @note History: 5/29/89, DSJ, Created. */ SAMPLE* MakeSample(CLUSTERER * Clusterer, const FLOAT32* Feature, inT32 CharID) { @@ -490,7 +489,6 @@ SAMPLE* MakeSample(CLUSTERER * Clusterer, const FLOAT32* Feature, return (Sample); } // MakeSample - /** * This routine first checks to see if the samples in this * clusterer have already been clustered before; if so, it does @@ -505,12 +503,12 @@ SAMPLE* MakeSample(CLUSTERER * Clusterer, const FLOAT32* Feature, * list of prototypes that best represent the samples given * the constraints specified in Config. * - * @param Clusterer data struct containing samples to be clustered - * @param Config parameters which control clustering process + * @param Clusterer data struct containing samples to be clustered + * @param Config parameters which control clustering process * * @return Pointer to a list of prototypes - * @note Exceptions: None - * @note History: 5/29/89, DSJ, Created. + * @note Exceptions: None + * @note History: 5/29/89, DSJ, Created. */ LIST ClusterSamples(CLUSTERER *Clusterer, CLUSTERCONFIG *Config) { //only create cluster tree if samples have never been clustered before @@ -523,10 +521,16 @@ LIST ClusterSamples(CLUSTERER *Clusterer, CLUSTERCONFIG *Config) { //compute prototypes starting at the root node in the tree ComputePrototypes(Clusterer, Config); - return (Clusterer->ProtoList); + // We don't need the cluster pointers in the protos any more, so null them + // out, which makes it safe to delete the clusterer. + LIST proto_list = Clusterer->ProtoList; + iterate(proto_list) { + PROTOTYPE *proto = reinterpret_cast(first_node(proto_list)); + proto->Cluster = NULL; + } + return Clusterer->ProtoList; } // ClusterSamples - /** * This routine frees all of the memory allocated to the * specified data structure. It will not, however, free @@ -535,10 +539,10 @@ LIST ClusterSamples(CLUSTERER *Clusterer, CLUSTERCONFIG *Config) { * to NULL to indicate that the cluster data structures no * longer exist. Any sample lists that have been obtained * via calls to GetSamples are no longer valid. - * @param Clusterer pointer to data structure to be freed + * @param Clusterer pointer to data structure to be freed * @return None - * @note Exceptions: None - * @note History: 6/6/89, DSJ, Created. + * @note Exceptions: None + * @note History: 6/6/89, DSJ, Created. */ void FreeClusterer(CLUSTERER *Clusterer) { if (Clusterer != NULL) { @@ -558,21 +562,19 @@ void FreeClusterer(CLUSTERER *Clusterer) { } } // FreeClusterer - /** * This routine frees all of the memory allocated to the * specified list of prototypes. The clusters which are * pointed to by the prototypes are not freed. - * @param ProtoList pointer to list of prototypes to be freed + * @param ProtoList pointer to list of prototypes to be freed * @return None - * @note Exceptions: None - * @note History: 6/6/89, DSJ, Created. + * @note Exceptions: None + * @note History: 6/6/89, DSJ, Created. */ void FreeProtoList(LIST *ProtoList) { destroy_nodes(*ProtoList, FreePrototype); } // FreeProtoList - /** * This routine deallocates the memory consumed by the specified * prototype and modifies the corresponding cluster so that it @@ -606,7 +608,6 @@ void FreePrototype(void *arg) { //PROTOTYPE *Prototype) memfree(Prototype); } // FreePrototype - /** * This routine is used to find all of the samples which * belong to a cluster. It starts by removing the top @@ -617,10 +618,10 @@ void FreePrototype(void *arg) { //PROTOTYPE *Prototype) * If all samples have been found, NULL is returned. * InitSampleSearch() must be called * before NextSample() to initialize the search. - * @param SearchState ptr to list containing clusters to be searched - * @return Pointer to the next leaf cluster (sample) or NULL. - * @note Exceptions: None - * @note History: 6/16/89, DSJ, Created. + * @param SearchState ptr to list containing clusters to be searched + * @return Pointer to the next leaf cluster (sample) or NULL. + * @note Exceptions: None + * @note History: 6/16/89, DSJ, Created. */ CLUSTER *NextSample(LIST *SearchState) { CLUSTER *Cluster; @@ -637,29 +638,27 @@ CLUSTER *NextSample(LIST *SearchState) { } } // NextSample - /** * This routine returns the mean of the specified * prototype in the indicated dimension. - * @param Proto prototype to return mean of - * @param Dimension dimension whose mean is to be returned - * @return Mean of Prototype in Dimension + * @param Proto prototype to return mean of + * @param Dimension dimension whose mean is to be returned + * @return Mean of Prototype in Dimension * @note Exceptions: none - * @note History: 7/6/89, DSJ, Created. + * @note History: 7/6/89, DSJ, Created. */ FLOAT32 Mean(PROTOTYPE *Proto, uinT16 Dimension) { return (Proto->Mean[Dimension]); } // Mean - /** * This routine returns the standard deviation of the * prototype in the indicated dimension. - * @param Proto prototype to return standard deviation of - * @param Dimension dimension whose stddev is to be returned - * @return Standard deviation of Prototype in Dimension + * @param Proto prototype to return standard deviation of + * @param Dimension dimension whose stddev is to be returned + * @return Standard deviation of Prototype in Dimension * @note Exceptions: none - * @note History: 7/6/89, DSJ, Created. + * @note History: 7/6/89, DSJ, Created. */ FLOAT32 StandardDeviation(PROTOTYPE *Proto, uinT16 Dimension) { switch (Proto->Style) { @@ -697,10 +696,10 @@ FLOAT32 StandardDeviation(PROTOTYPE *Proto, uinT16 Dimension) { * tree are the individual samples themselves; they have no * sub-clusters. The root node of the tree conceptually contains * all of the samples. - * @param Clusterer data structure holdings samples to be clustered - * @return None (the Clusterer data structure is changed) - * @note Exceptions: None - * @note History: 5/29/89, DSJ, Created. + * @param Clusterer data structure holdings samples to be clustered + * @return None (the Clusterer data structure is changed) + * @note Exceptions: None + * @note History: 5/29/89, DSJ, Created. */ void CreateClusterTree(CLUSTERER *Clusterer) { ClusteringContext context; @@ -760,7 +759,6 @@ void CreateClusterTree(CLUSTERER *Clusterer) { memfree(context.candidates); } // CreateClusterTree - /** * This routine is designed to be used in concert with the * KDWalk routine. It will create a potential cluster for @@ -786,7 +784,6 @@ void MakePotentialClusters(ClusteringContext *context, } } // MakePotentialClusters - /** * This routine searches the specified kd-tree for the nearest * neighbor of the specified cluster. It actually uses the @@ -795,12 +792,12 @@ void MakePotentialClusters(ClusteringContext *context, * neighbor is returned, if it can be found, otherwise NULL is * returned. The distance between the 2 nodes is placed * in the specified variable. - * @param Tree kd-tree to search in for nearest neighbor - * @param Cluster cluster whose nearest neighbor is to be found - * @param Distance ptr to variable to report distance found - * @return Pointer to the nearest neighbor of Cluster, or NULL + * @param Tree kd-tree to search in for nearest neighbor + * @param Cluster cluster whose nearest neighbor is to be found + * @param Distance ptr to variable to report distance found + * @return Pointer to the nearest neighbor of Cluster, or NULL * @note Exceptions: none - * @note History: 5/29/89, DSJ, Created. + * @note History: 5/29/89, DSJ, Created. * 7/13/89, DSJ, Removed visibility of kd-tree node data struct */ CLUSTER * @@ -830,17 +827,16 @@ FindNearestNeighbor(KDTREE * Tree, CLUSTER * Cluster, FLOAT32 * Distance) return BestNeighbor; } // FindNearestNeighbor - /** * This routine creates a new permanent cluster from the * clusters specified in TempCluster. The 2 clusters in * TempCluster are marked as "clustered" and deleted from * the kd-tree. The new cluster is then added to the kd-tree. - * @param Clusterer current clustering environment - * @param TempCluster potential cluster to make permanent + * @param Clusterer current clustering environment + * @param TempCluster potential cluster to make permanent * @return Pointer to the new permanent cluster - * @note Exceptions: none - * @note History: 5/29/89, DSJ, Created. + * @note Exceptions: none + * @note History: 5/29/89, DSJ, Created. * 7/13/89, DSJ, Removed visibility of kd-tree node data struct */ CLUSTER *MakeNewCluster(CLUSTERER *Clusterer, TEMPCLUSTER *TempCluster) { @@ -872,21 +868,20 @@ CLUSTER *MakeNewCluster(CLUSTERER *Clusterer, TEMPCLUSTER *TempCluster) { return Cluster; } // MakeNewCluster - /** * This routine merges two clusters into one larger cluster. * To do this it computes the number of samples in the new * cluster and the mean of the new cluster. The ParamDesc * information is used to ensure that circular dimensions * are handled correctly. - * @param N # of dimensions (size of arrays) - * @param ParamDesc array of dimension descriptions - * @param n1, n2 number of samples in each old cluster - * @param m array to hold mean of new cluster - * @param m1, m2 arrays containing means of old clusters - * @return The number of samples in the new cluster. - * @note Exceptions: None - * @note History: 5/31/89, DSJ, Created. + * @param N # of dimensions (size of arrays) + * @param ParamDesc array of dimension descriptions + * @param n1, n2 number of samples in each old cluster + * @param m array to hold mean of new cluster + * @param m1, m2 arrays containing means of old clusters + * @return The number of samples in the new cluster. + * @note Exceptions: None + * @note History: 5/31/89, DSJ, Created. */ inT32 MergeClusters(inT16 N, PARAM_DESC ParamDesc[], @@ -921,17 +916,16 @@ inT32 MergeClusters(inT16 N, return n; } // MergeClusters - /** * This routine decides which clusters in the cluster tree * should be represented by prototypes, forms a list of these * prototypes, and places the list in the Clusterer data * structure. - * @param Clusterer data structure holding cluster tree - * @param Config parameters used to control prototype generation - * @return None - * @note Exceptions: None - * @note History: 5/30/89, DSJ, Created. + * @param Clusterer data structure holding cluster tree + * @param Config parameters used to control prototype generation + * @return None + * @note Exceptions: None + * @note History: 5/30/89, DSJ, Created. */ void ComputePrototypes(CLUSTERER *Clusterer, CLUSTERCONFIG *Config) { LIST ClusterStack = NIL_LIST; @@ -961,8 +955,7 @@ void ComputePrototypes(CLUSTERER *Clusterer, CLUSTERCONFIG *Config) { } } // ComputePrototypes - -/** +/** * This routine attempts to create a prototype from the * specified cluster that conforms to the distribution * specified in Config. If there are too few samples in the @@ -972,12 +965,12 @@ void ComputePrototypes(CLUSTERER *Clusterer, CLUSTERCONFIG *Config) { * is generated and NULL is returned. If a prototype can be * found that matches the desired distribution then a pointer * to it is returned, otherwise NULL is returned. - * @param Clusterer data structure holding cluster tree - * @param Config parameters used to control prototype generation - * @param Cluster cluster to be made into a prototype - * @return Pointer to new prototype or NULL - * @note Exceptions: None - * @note History: 6/19/89, DSJ, Created. + * @param Clusterer data structure holding cluster tree + * @param Config parameters used to control prototype generation + * @param Cluster cluster to be made into a prototype + * @return Pointer to new prototype or NULL + * @note Exceptions: None + * @note History: 6/19/89, DSJ, Created. */ PROTOTYPE *MakePrototype(CLUSTERER *Clusterer, CLUSTERCONFIG *Config, @@ -1050,7 +1043,6 @@ PROTOTYPE *MakePrototype(CLUSTERER *Clusterer, return Proto; } // MakePrototype - /** * This routine checks for clusters which are degenerate and * therefore cannot be analyzed in a statistically valid way. @@ -1063,14 +1055,14 @@ PROTOTYPE *MakePrototype(CLUSTERER *Clusterer, * * If the cluster is not degenerate, NULL is returned. * - * @param N number of dimensions - * @param Cluster cluster being analyzed - * @param Statistics statistical info about cluster - * @param Style type of prototype to be generated - * @param MinSamples minimum number of samples in a cluster - * @return Pointer to degenerate prototype or NULL. - * @note Exceptions: None - * @note History: 6/20/89, DSJ, Created. + * @param N number of dimensions + * @param Cluster cluster being analyzed + * @param Statistics statistical info about cluster + * @param Style type of prototype to be generated + * @param MinSamples minimum number of samples in a cluster + * @return Pointer to degenerate prototype or NULL. + * @note Exceptions: None + * @note History: 6/20/89, DSJ, Created. * 7/12/89, DSJ, Changed name and added check for 0 stddev. * 8/8/89, DSJ, Removed check for 0 stddev (handled elsewhere). */ @@ -1110,10 +1102,10 @@ PROTOTYPE *MakeDegenerateProto( //this was MinSample * be split. If not, then a new prototype is formed and * returned to the caller. If there is, then NULL is returned * to the caller. - * @param Clusterer data struct containing samples being clustered + * @param Clusterer data struct containing samples being clustered * @param Config provides the magic number of samples that make a good cluster - * @param Cluster cluster to be made into an elliptical prototype - * @param Statistics statistical info about cluster + * @param Cluster cluster to be made into an elliptical prototype + * @param Statistics statistical info about cluster * @return Pointer to new elliptical prototype or NULL. */ PROTOTYPE *TestEllipticalProto(CLUSTERER *Clusterer, @@ -1215,13 +1207,13 @@ PROTOTYPE *TestEllipticalProto(CLUSTERER *Clusterer, * be approximated by a spherical normal distribution. If it * can be, then a new prototype is formed and returned to the * caller. If it can't be, then NULL is returned to the caller. - * @param Clusterer data struct containing samples being clustered - * @param Cluster cluster to be made into a spherical prototype - * @param Statistics statistical info about cluster - * @param Buckets histogram struct used to analyze distribution - * @return Pointer to new spherical prototype or NULL. - * @note Exceptions: None - * @note History: 6/1/89, DSJ, Created. + * @param Clusterer data struct containing samples being clustered + * @param Cluster cluster to be made into a spherical prototype + * @param Statistics statistical info about cluster + * @param Buckets histogram struct used to analyze distribution + * @return Pointer to new spherical prototype or NULL. + * @note Exceptions: None + * @note History: 6/1/89, DSJ, Created. */ PROTOTYPE *MakeSphericalProto(CLUSTERER *Clusterer, CLUSTER *Cluster, @@ -1247,19 +1239,18 @@ PROTOTYPE *MakeSphericalProto(CLUSTERER *Clusterer, return (Proto); } // MakeSphericalProto - /** * This routine tests the specified cluster to see if it can * be approximated by an elliptical normal distribution. If it * can be, then a new prototype is formed and returned to the * caller. If it can't be, then NULL is returned to the caller. - * @param Clusterer data struct containing samples being clustered - * @param Cluster cluster to be made into an elliptical prototype - * @param Statistics statistical info about cluster - * @param Buckets histogram struct used to analyze distribution - * @return Pointer to new elliptical prototype or NULL. - * @note Exceptions: None - * @note History: 6/12/89, DSJ, Created. + * @param Clusterer data struct containing samples being clustered + * @param Cluster cluster to be made into an elliptical prototype + * @param Statistics statistical info about cluster + * @param Buckets histogram struct used to analyze distribution + * @return Pointer to new elliptical prototype or NULL. + * @note Exceptions: None + * @note History: 6/12/89, DSJ, Created. */ PROTOTYPE *MakeEllipticalProto(CLUSTERER *Clusterer, CLUSTER *Cluster, @@ -1286,7 +1277,6 @@ PROTOTYPE *MakeEllipticalProto(CLUSTERER *Clusterer, return (Proto); } // MakeEllipticalProto - /** * This routine tests each dimension of the specified cluster to * see what distribution would best approximate that dimension. @@ -1295,14 +1285,14 @@ PROTOTYPE *MakeEllipticalProto(CLUSTERER *Clusterer, * be represented by one of these distributions, * then a new prototype is formed and returned to the * caller. If it can't be, then NULL is returned to the caller. - * @param Clusterer data struct containing samples being clustered - * @param Cluster cluster to be made into a prototype - * @param Statistics statistical info about cluster - * @param NormalBuckets histogram struct used to analyze distribution - * @param Confidence confidence level for alternate distributions - * @return Pointer to new mixed prototype or NULL. - * @note Exceptions: None - * @note History: 6/12/89, DSJ, Created. + * @param Clusterer data struct containing samples being clustered + * @param Cluster cluster to be made into a prototype + * @param Statistics statistical info about cluster + * @param NormalBuckets histogram struct used to analyze distribution + * @param Confidence confidence level for alternate distributions + * @return Pointer to new mixed prototype or NULL. + * @note Exceptions: None + * @note History: 6/12/89, DSJ, Created. */ PROTOTYPE *MakeMixedProto(CLUSTERER *Clusterer, CLUSTER *Cluster, @@ -1355,16 +1345,15 @@ PROTOTYPE *MakeMixedProto(CLUSTERER *Clusterer, return (Proto); } // MakeMixedProto - /** * This routine alters the ith dimension of the specified * mixed prototype to be D_random. - * @param i index of dimension to be changed - * @param Proto prototype whose dimension is to be altered - * @param ParamDesc description of specified dimension - * @return None - * @note Exceptions: None - * @note History: 6/20/89, DSJ, Created. + * @param i index of dimension to be changed + * @param Proto prototype whose dimension is to be altered + * @param ParamDesc description of specified dimension + * @return None + * @note Exceptions: None + * @note History: 6/20/89, DSJ, Created. */ void MakeDimRandom(uinT16 i, PROTOTYPE *Proto, PARAM_DESC *ParamDesc) { Proto->Distrib[i] = D_random; @@ -1380,16 +1369,15 @@ void MakeDimRandom(uinT16 i, PROTOTYPE *Proto, PARAM_DESC *ParamDesc) { // note that the proto Weight is irrelevant for D_random protos } // MakeDimRandom - /** * This routine alters the ith dimension of the specified * mixed prototype to be uniform. - * @param i index of dimension to be changed - * @param Proto prototype whose dimension is to be altered - * @param Statistics statistical info about prototype - * @return None - * @note Exceptions: None - * @note History: 6/20/89, DSJ, Created. + * @param i index of dimension to be changed + * @param Proto prototype whose dimension is to be altered + * @param Statistics statistical info about prototype + * @return None + * @note Exceptions: None + * @note History: 6/20/89, DSJ, Created. */ void MakeDimUniform(uinT16 i, PROTOTYPE *Proto, STATISTICS *Statistics) { Proto->Distrib[i] = uniform; @@ -1410,7 +1398,6 @@ void MakeDimUniform(uinT16 i, PROTOTYPE *Proto, STATISTICS *Statistics) { // note that the proto Weight is irrelevant for uniform protos } // MakeDimUniform - /** * This routine searches the cluster tree for all leaf nodes * which are samples in the specified cluster. It computes @@ -1420,12 +1407,12 @@ void MakeDimUniform(uinT16 i, PROTOTYPE *Proto, STATISTICS *Statistics) { * return this information to the caller. An incremental * algorithm for computing statistics is not used because * it will not work with circular dimensions. - * @param N number of dimensions - * @param ParamDesc array of dimension descriptions - * @param Cluster cluster whose stats are to be computed - * @return Pointer to new data structure containing statistics - * @note Exceptions: None - * @note History: 6/2/89, DSJ, Created. + * @param N number of dimensions + * @param ParamDesc array of dimension descriptions + * @param Cluster cluster whose stats are to be computed + * @return Pointer to new data structure containing statistics + * @note Exceptions: None + * @note History: 6/2/89, DSJ, Created. */ STATISTICS * ComputeStatistics (inT16 N, PARAM_DESC ParamDesc[], CLUSTER * Cluster) { @@ -1502,19 +1489,18 @@ ComputeStatistics (inT16 N, PARAM_DESC ParamDesc[], CLUSTER * Cluster) { return (Statistics); } // ComputeStatistics - /** * This routine creates a spherical prototype data structure to * approximate the samples in the specified cluster. * Spherical prototypes have a single variance which is * common across all dimensions. All dimensions are normally * distributed and independent. - * @param N number of dimensions - * @param Cluster cluster to be made into a spherical prototype - * @param Statistics statistical info about samples in cluster - * @return Pointer to a new spherical prototype data structure - * @note Exceptions: None - * @note History: 6/19/89, DSJ, Created. + * @param N number of dimensions + * @param Cluster cluster to be made into a spherical prototype + * @param Statistics statistical info about samples in cluster + * @return Pointer to a new spherical prototype data structure + * @note Exceptions: None + * @note History: 6/19/89, DSJ, Created. */ PROTOTYPE *NewSphericalProto(uinT16 N, CLUSTER *Cluster, @@ -1537,18 +1523,17 @@ PROTOTYPE *NewSphericalProto(uinT16 N, return (Proto); } // NewSphericalProto - /** * This routine creates an elliptical prototype data structure to * approximate the samples in the specified cluster. * Elliptical prototypes have a variance for each dimension. * All dimensions are normally distributed and independent. - * @param N number of dimensions - * @param Cluster cluster to be made into an elliptical prototype - * @param Statistics statistical info about samples in cluster - * @return Pointer to a new elliptical prototype data structure - * @note Exceptions: None - * @note History: 6/19/89, DSJ, Created. + * @param N number of dimensions + * @param Cluster cluster to be made into an elliptical prototype + * @param Statistics statistical info about samples in cluster + * @return Pointer to a new elliptical prototype data structure + * @note Exceptions: None + * @note History: 6/19/89, DSJ, Created. */ PROTOTYPE *NewEllipticalProto(inT16 N, CLUSTER *Cluster, @@ -1579,7 +1564,6 @@ PROTOTYPE *NewEllipticalProto(inT16 N, return (Proto); } // NewEllipticalProto - /** * This routine creates a mixed prototype data structure to * approximate the samples in the specified cluster. @@ -1588,12 +1572,12 @@ PROTOTYPE *NewEllipticalProto(inT16 N, * structure is initially filled in as though it were an * elliptical prototype. The actual distributions of the * dimensions can be altered by other routines. - * @param N number of dimensions - * @param Cluster cluster to be made into a mixed prototype - * @param Statistics statistical info about samples in cluster - * @return Pointer to a new mixed prototype data structure - * @note Exceptions: None - * @note History: 6/19/89, DSJ, Created. + * @param N number of dimensions + * @param Cluster cluster to be made into a mixed prototype + * @param Statistics statistical info about samples in cluster + * @return Pointer to a new mixed prototype data structure + * @note Exceptions: None + * @note History: 6/19/89, DSJ, Created. */ PROTOTYPE *NewMixedProto(inT16 N, CLUSTER *Cluster, STATISTICS *Statistics) { PROTOTYPE *Proto; @@ -1609,16 +1593,15 @@ PROTOTYPE *NewMixedProto(inT16 N, CLUSTER *Cluster, STATISTICS *Statistics) { return (Proto); } // NewMixedProto - /** * This routine allocates memory to hold a simple prototype * data structure, i.e. one without independent distributions * and variances for each dimension. - * @param N number of dimensions - * @param Cluster cluster to be made into a prototype - * @return Pointer to new simple prototype - * @note Exceptions: None - * @note History: 6/19/89, DSJ, Created. + * @param N number of dimensions + * @param Cluster cluster to be made into a prototype + * @return Pointer to new simple prototype + * @note Exceptions: None + * @note History: 6/19/89, DSJ, Created. */ PROTOTYPE *NewSimpleProto(inT16 N, CLUSTER *Cluster) { PROTOTYPE *Proto; @@ -1640,7 +1623,6 @@ PROTOTYPE *NewSimpleProto(inT16 N, CLUSTER *Cluster) { return (Proto); } // NewSimpleProto - /** * This routine returns TRUE if the specified covariance * matrix indicates that all N dimensions are independent of @@ -1653,13 +1635,13 @@ PROTOTYPE *NewSimpleProto(inT16 N, CLUSTER *Cluster) { * coeff[ij] = stddev[ij] / sqrt (stddev[ii] * stddev[jj]) * The covariance matrix is assumed to be symmetric (which * should always be true). - * @param ParamDesc descriptions of each feature space dimension - * @param N number of dimensions - * @param CoVariance ptr to a covariance matrix - * @param Independence max off-diagonal correlation coefficient - * @return TRUE if dimensions are independent, FALSE otherwise - * @note Exceptions: None - * @note History: 6/4/89, DSJ, Created. + * @param ParamDesc descriptions of each feature space dimension + * @param N number of dimensions + * @param CoVariance ptr to a covariance matrix + * @param Independence max off-diagonal correlation coefficient + * @return TRUE if dimensions are independent, FALSE otherwise + * @note Exceptions: None + * @note History: 6/4/89, DSJ, Created. */ BOOL8 Independent (PARAM_DESC ParamDesc[], @@ -1692,7 +1674,6 @@ inT16 N, FLOAT32 * CoVariance, FLOAT32 Independence) { return (TRUE); } // Independent - /** * This routine returns a histogram data structure which can * be used by other routines to place samples into histogram @@ -1703,12 +1684,12 @@ inT16 N, FLOAT32 * CoVariance, FLOAT32 Independence) { * created so that it minimizes the computation time needed * to create a new bucket. * @param clusterer which keeps a bucket_cache for us. - * @param Distribution type of probability distribution to test for - * @param SampleCount number of samples that are available - * @param Confidence probability of a Type I error - * @return Bucket data structure + * @param Distribution type of probability distribution to test for + * @param SampleCount number of samples that are available + * @param Confidence probability of a Type I error + * @return Bucket data structure * @note Exceptions: none - * @note History: Thu Aug 3 12:58:10 1989, DSJ, Created. + * @note History: Thu Aug 3 12:58:10 1989, DSJ, Created. */ BUCKETS *GetBuckets(CLUSTERER* clusterer, DISTRIBUTION Distribution, @@ -1739,7 +1720,6 @@ BUCKETS *GetBuckets(CLUSTERER* clusterer, return Buckets; } // GetBuckets - /** * This routine creates a histogram data structure which can * be used by other routines to place samples into histogram @@ -1751,12 +1731,12 @@ BUCKETS *GetBuckets(CLUSTERER* clusterer, * order to make this possible, a mapping table is * computed which maps "normalized" samples into the * appropriate bucket. - * @param Distribution type of probability distribution to test for - * @param SampleCount number of samples that are available - * @param Confidence probability of a Type I error + * @param Distribution type of probability distribution to test for + * @param SampleCount number of samples that are available + * @param Confidence probability of a Type I error * @return Pointer to new histogram data structure - * @note Exceptions: None - * @note History: 6/4/89, DSJ, Created. + * @note Exceptions: None + * @note History: 6/4/89, DSJ, Created. */ BUCKETS *MakeBuckets(DISTRIBUTION Distribution, uinT32 SampleCount, @@ -1840,7 +1820,6 @@ BUCKETS *MakeBuckets(DISTRIBUTION Distribution, return Buckets; } // MakeBuckets - /** * This routine computes the optimum number of histogram * buckets that should be used in a chi-squared goodness of @@ -1851,7 +1830,7 @@ BUCKETS *MakeBuckets(DISTRIBUTION Distribution, * values. The table is intended for a 0.05 level of * significance (alpha). This routine assumes that it is * equally valid for other alpha's, which may not be true. - * @param SampleCount number of samples to be tested + * @param SampleCount number of samples to be tested * @return Optimum number of histogram buckets * @note Exceptions: None * @note History: 6/5/89, DSJ, Created. @@ -1874,7 +1853,6 @@ uinT16 OptimumNumberOfBuckets(uinT32 SampleCount) { return kBucketsTable[Last]; } // OptimumNumberOfBuckets - /** * This routine computes the chi-squared value which will * leave a cumulative probability of Alpha in the right tail @@ -1887,8 +1865,8 @@ uinT16 OptimumNumberOfBuckets(uinT32 SampleCount) { * chi-squared value. Therefore, once a particular chi-squared * value is computed, it is stored in the list and never * needs to be computed again. - * @param DegreesOfFreedom determines shape of distribution - * @param Alpha probability of right tail + * @param DegreesOfFreedom determines shape of distribution + * @param Alpha probability of right tail * @return Desired chi-squared value * @note Exceptions: none * @note History: 6/5/89, DSJ, Created. @@ -1932,19 +1910,19 @@ ComputeChiSquared (uinT16 DegreesOfFreedom, FLOAT64 Alpha) } // ComputeChiSquared - /** * This routine computes the probability density function * of a discrete normal distribution defined by the global * variables kNormalMean, kNormalVariance, and kNormalMagnitude. * Normal magnitude could, of course, be computed in terms of * the normal variance but it is precomputed for efficiency. - * @param x number to compute the normal probability density for + * @param x number to compute the normal probability density for * @note Globals: - * kNormalMean mean of a discrete normal distribution - * kNormalVariance variance of a discrete normal distribution - * kNormalMagnitude magnitude of a discrete normal distribution - * @return The value of the normal distribution at x. + * kNormalMean mean of a discrete normal distribution + * kNormalVariance variance of a discrete normal distribution + * kNormalMagnitude magnitude of a discrete normal + *distribution + * @return The value of the normal distribution at x. * @note Exceptions: None * @note History: 6/4/89, DSJ, Created. */ @@ -1955,12 +1933,11 @@ FLOAT64 NormalDensity(inT32 x) { return kNormalMagnitude * exp(-0.5 * Distance * Distance / kNormalVariance); } // NormalDensity - /** * This routine computes the probability density function * of a uniform distribution at the specified point. The * range of the distribution is from 0 to BUCKETTABLESIZE. - * @param x number to compute the uniform probability density for + * @param x number to compute the uniform probability density for * @return The value of the uniform distribution at x. * @note Exceptions: None * @note History: 6/5/89, DSJ, Created. @@ -1974,13 +1951,12 @@ FLOAT64 UniformDensity(inT32 x) { return (FLOAT64) 0.0; } // UniformDensity - /** * This routine computes a trapezoidal approximation to the * integral of a function over a small delta in x. - * @param f1 value of function at x1 - * @param f2 value of function at x2 - * @param Dx x2 - x1 (should always be positive) + * @param f1 value of function at x1 + * @param f2 value of function at x2 + * @param Dx x2 - x1 (should always be positive) * @return Approximation of the integral of the function from x1 to x2. * @note Exceptions: None * @note History: 6/5/89, DSJ, Created. @@ -1989,7 +1965,6 @@ FLOAT64 Integral(FLOAT64 f1, FLOAT64 f2, FLOAT64 Dx) { return (f1 + f2) * Dx / 2.0; } // Integral - /** * This routine counts the number of cluster samples which * fall within the various histogram buckets in Buckets. Only @@ -2002,12 +1977,12 @@ FLOAT64 Integral(FLOAT64 f1, FLOAT64 f2, FLOAT64 Dx) { * range and the StdDev is 1/2 the range. A dimension with * zero standard deviation cannot be statistically analyzed. * In this case, a pseudo-analysis is used. - * @param Buckets histogram buckets to count samples - * @param Cluster cluster whose samples are being analyzed - * @param Dim dimension of samples which is being analyzed - * @param ParamDesc description of the dimension - * @param Mean "mean" of the distribution - * @param StdDev "standard deviation" of the distribution + * @param Buckets histogram buckets to count samples + * @param Cluster cluster whose samples are being analyzed + * @param Dim dimension of samples which is being analyzed + * @param ParamDesc description of the dimension + * @param Mean "mean" of the distribution + * @param StdDev "standard deviation" of the distribution * @return None (the Buckets data structure is filled in) * @note Exceptions: None * @note History: 6/5/89, DSJ, Created. @@ -2071,16 +2046,15 @@ void FillBuckets(BUCKETS *Buckets, } } // FillBuckets - /** * This routine determines which bucket x falls into in the * discrete normal distribution defined by kNormalMean * and kNormalStdDev. x values which exceed the range of * the discrete distribution are clipped. - * @param ParamDesc used to identify circular dimensions - * @param x value to be normalized - * @param Mean mean of normal distribution - * @param StdDev standard deviation of normal distribution + * @param ParamDesc used to identify circular dimensions + * @param x value to be normalized + * @param Mean mean of normal distribution + * @param StdDev standard deviation of normal distribution * @return Bucket number into which x falls * @note Exceptions: None * @note History: 6/5/89, DSJ, Created. @@ -2107,16 +2081,15 @@ uinT16 NormalBucket(PARAM_DESC *ParamDesc, return (uinT16) floor((FLOAT64) X); } // NormalBucket - /** * This routine determines which bucket x falls into in the * discrete uniform distribution defined by * BUCKETTABLESIZE. x values which exceed the range of * the discrete distribution are clipped. - * @param ParamDesc used to identify circular dimensions - * @param x value to be normalized - * @param Mean center of range of uniform distribution - * @param StdDev 1/2 the range of the uniform distribution + * @param ParamDesc used to identify circular dimensions + * @param x value to be normalized + * @param Mean center of range of uniform distribution + * @param StdDev 1/2 the range of the uniform distribution * @return Bucket number into which x falls * @note Exceptions: None * @note History: 6/5/89, DSJ, Created. @@ -2143,7 +2116,6 @@ uinT16 UniformBucket(PARAM_DESC *ParamDesc, return (uinT16) floor((FLOAT64) X); } // UniformBucket - /** * This routine performs a chi-square goodness of fit test * on the histogram data in the Buckets data structure. TRUE @@ -2151,7 +2123,7 @@ uinT16 UniformBucket(PARAM_DESC *ParamDesc, * distribution which was specified when the Buckets * structure was originally created. Otherwise FALSE is * returned. - * @param Buckets histogram data to perform chi-square test on + * @param Buckets histogram data to perform chi-square test on * @return TRUE if samples match distribution, FALSE otherwise * @note Exceptions: None * @note History: 6/5/89, DSJ, Created. @@ -2176,11 +2148,10 @@ BOOL8 DistributionOK(BUCKETS *Buckets) { return TRUE; } // DistributionOK - /** * This routine frees the memory used by the statistics * data structure. - * @param Statistics pointer to data structure to be freed + * @param Statistics pointer to data structure to be freed * @return None * @note Exceptions: None * @note History: 6/5/89, DSJ, Created. @@ -2192,7 +2163,6 @@ void FreeStatistics(STATISTICS *Statistics) { memfree(Statistics); } // FreeStatistics - /** * This routine properly frees the memory used by a BUCKETS. * @@ -2204,13 +2174,12 @@ void FreeBuckets(BUCKETS *buckets) { Efree(buckets); } // FreeBuckets - /** * This routine frees the memory consumed by the specified * cluster and all of its subclusters. This is done by * recursive calls to FreeCluster(). * - * @param Cluster pointer to cluster to be freed + * @param Cluster pointer to cluster to be freed * * @return None * @@ -2225,7 +2194,6 @@ void FreeCluster(CLUSTER *Cluster) { } } // FreeCluster - /** * This routine computes the degrees of freedom that should * be used in a chi-squared test with the specified number of @@ -2234,8 +2202,8 @@ void FreeCluster(CLUSTER *Cluster) { * computed more easily. This will cause the value of * chi-squared to be higher than the optimum value, resulting * in the chi-square test being more lenient than optimum. - * @param Distribution distribution being tested for - * @param HistogramBuckets number of buckets in chi-square test + * @param Distribution distribution being tested for + * @param HistogramBuckets number of buckets in chi-square test * @return The number of degrees of freedom for a chi-square test * @note Exceptions: none * @note History: Thu Aug 3 14:04:18 1989, DSJ, Created. @@ -2252,7 +2220,6 @@ uinT16 DegreesOfFreedom(DISTRIBUTION Distribution, uinT16 HistogramBuckets) { } // DegreesOfFreedom - /** * This routine is used to search a list of histogram data * structures to find one with the specified number of @@ -2272,7 +2239,6 @@ int NumBucketsMatch(void *arg1, // BUCKETS *Histogram, } // NumBucketsMatch - /** * This routine is used to search a list for a list node * whose contents match Key. It is called by the list @@ -2287,13 +2253,12 @@ int ListEntryMatch(void *arg1, //ListNode } // ListEntryMatch - /** * This routine multiplies each ExpectedCount histogram entry * by NewSampleCount/OldSampleCount so that the histogram * is now adjusted to the new sample count. - * @param Buckets histogram data structure to adjust - * @param NewSampleCount new sample count to adjust to + * @param Buckets histogram data structure to adjust + * @param NewSampleCount new sample count to adjust to * @return none * @note Exceptions: none * @note History: Thu Aug 3 14:31:14 1989, DSJ, Created. @@ -2313,11 +2278,10 @@ void AdjustBuckets(BUCKETS *Buckets, uinT32 NewSampleCount) { } // AdjustBuckets - /** * This routine sets the bucket counts in the specified histogram * to zero. - * @param Buckets histogram data structure to init + * @param Buckets histogram data structure to init * @return none * @note Exceptions: none * @note History: Thu Aug 3 14:31:14 1989, DSJ, Created. @@ -2331,7 +2295,6 @@ void InitBuckets(BUCKETS *Buckets) { } // InitBuckets - /** * This routine is used to search a list of structures which * hold pre-computed chi-squared values for a chi-squared @@ -2355,14 +2318,13 @@ int AlphaMatch(void *arg1, //CHISTRUCT *ChiStruct } // AlphaMatch - /** * This routine allocates a new data structure which is used * to hold a chi-squared value along with its associated * number of degrees of freedom and alpha value. * - * @param DegreesOfFreedom degrees of freedom for new chi value - * @param Alpha confidence level for new chi value + * @param DegreesOfFreedom degrees of freedom for new chi value + * @param Alpha confidence level for new chi value * @return none * @note Exceptions: none * @note History: Fri Aug 4 11:04:59 1989, DSJ, Created. @@ -2377,7 +2339,6 @@ CHISTRUCT *NewChiStruct(uinT16 DegreesOfFreedom, FLOAT64 Alpha) { } // NewChiStruct - /** * This routine attempts to find an x value at which Function * goes to zero (i.e. a root of the function ). It will only @@ -2385,10 +2346,10 @@ CHISTRUCT *NewChiStruct(uinT16 DegreesOfFreedom, FLOAT64 Alpha) { * are no extrema between the solution and the InitialGuess. * The algorithms used are extremely primitive. * - * @param Function function whose zero is to be found - * @param FunctionParams arbitrary data to pass to function - * @param InitialGuess point to start solution search at - * @param Accuracy maximum allowed error + * @param Function function whose zero is to be found + * @param FunctionParams arbitrary data to pass to function + * @param InitialGuess point to start solution search at + * @param Accuracy maximum allowed error * @return Solution of function ( x for which f(x) = 0 ). * @note Exceptions: none * @note History: Fri Aug 4 11:08:59 1989, DSJ, Created. @@ -2440,7 +2401,6 @@ void *FunctionParams, FLOAT64 InitialGuess, FLOAT64 Accuracy) } // Solve - /** * This routine computes the area under a chi density curve * from 0 to x, minus the desired area under the curve. The @@ -2455,8 +2415,8 @@ void *FunctionParams, FLOAT64 InitialGuess, FLOAT64 Accuracy) * integrating the chi density curve in parts to obtain * a series that can be used to compute the area under the * curve. - * @param ChiParams contains degrees of freedom and alpha - * @param x value of chi-squared to evaluate + * @param ChiParams contains degrees of freedom and alpha + * @param x value of chi-squared to evaluate * @return Error between actual and desired area under the chi curve. * @note Exceptions: none * @note History: Fri Aug 4 12:48:41 1989, DSJ, Created. @@ -2480,7 +2440,6 @@ FLOAT64 ChiArea(CHISTRUCT *ChiParams, FLOAT64 x) { } // ChiArea - /** * This routine looks at all samples in the specified cluster. * It computes a running estimate of the percentage of the @@ -2498,10 +2457,10 @@ FLOAT64 ChiArea(CHISTRUCT *ChiParams, FLOAT64 x) { * contained in the same cluster, then the cluster should be * split. * - * @param Clusterer data structure holding cluster tree - * @param Cluster cluster containing samples to be tested - * @param MaxIllegal max percentage of samples allowed to have - * more than 1 feature in the cluster + * @param Clusterer data structure holding cluster tree + * @param Cluster cluster containing samples to be tested + * @param MaxIllegal max percentage of samples allowed to have + * more than 1 feature in the cluster * @return TRUE if the cluster should be split, FALSE otherwise. * @note Exceptions: none * @note History: Wed Aug 30 11:13:05 1989, DSJ, Created. @@ -2562,7 +2521,7 @@ CLUSTER * Cluster, FLOAT32 MaxIllegal) } // MultipleCharSamples /** - * Compute the inverse of a matrix using LU decomposition with partial pivoting. + * Compute the inverse of a matrix using LU decomposition with partial pivoting. * The return value is the sum of norms of the off-diagonal terms of the * product of a and inv. (A measure of the error.) */ diff --git a/classify/clusttool.cpp b/classify/clusttool.cpp index d86c3a240..02e619d27 100644 --- a/classify/clusttool.cpp +++ b/classify/clusttool.cpp @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: clustertool.c - ** Purpose: Misc. tools for use with the clustering routines - ** Author: Dan Johnson - ** History: 6/6/89, DSJ, Created. + ** Filename: clustertool.c + ** Purpose: Misc. tools for use with the clustering routines + ** Author: Dan Johnson + ** History: 6/6/89, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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 @@ -26,9 +26,10 @@ #include //---------------Global Data Definitions and Declarations-------------------- -#define TOKENSIZE 80 //< max size of tokens read from an input file -#define MAXSAMPLESIZE 65535 //< max num of dimensions in feature space -//#define MAXBLOCKSIZE 65535 //< max num of samples in a character (block size) +#define TOKENSIZE 80 //< max size of tokens read from an input file +#define MAXSAMPLESIZE 65535 //< max num of dimensions in feature space +//#define MAXBLOCKSIZE 65535 //< max num of samples in a character (block +// size) /** * This routine reads a single integer from the specified @@ -37,7 +38,7 @@ * @param File open text file to read sample size from * @return Sample size * @note Globals: None - * @note Exceptions: ILLEGALSAMPLESIZE illegal format or range + * @note Exceptions: ILLEGALSAMPLESIZE illegal format or range * @note History: 6/6/89, DSJ, Created. */ uinT16 ReadSampleSize(FILE *File) { @@ -293,7 +294,7 @@ FLOAT32* ReadNFloats(FILE * File, uinT16 N, FLOAT32 Buffer[]) { if (NumFloatsRead != 1) { if ((NumFloatsRead == EOF) && (i == 0)) { if (needs_free) { - Efree(Buffer); + Efree(Buffer); } return NULL; } else { @@ -315,8 +316,7 @@ FLOAT32* ReadNFloats(FILE * File, uinT16 N, FLOAT32 Buffer[]) { * @note Exceptions: None * @note History: 6/6/89, DSJ, Created. */ -void -WriteParamDesc (FILE * File, uinT16 N, PARAM_DESC ParamDesc[]) { +void WriteParamDesc(FILE *File, uinT16 N, const PARAM_DESC ParamDesc[]) { int i; for (i = 0; i < N; i++) { @@ -446,15 +446,10 @@ void WriteProtoStyle(FILE *File, PROTOSTYLE ProtoStyle) { * @note History: 6/12/89, DSJ, Created. */ -void WriteProtoList( - FILE *File, - uinT16 N, - PARAM_DESC ParamDesc[], - LIST ProtoList, - BOOL8 WriteSigProtos, - BOOL8 WriteInsigProtos) -{ - PROTOTYPE *Proto; +void WriteProtoList(FILE *File, uinT16 N, PARAM_DESC ParamDesc[], + LIST ProtoList, BOOL8 WriteSigProtos, + BOOL8 WriteInsigProtos) { + PROTOTYPE *Proto; /* write file header */ fprintf(File,"%0d\n",N); @@ -464,8 +459,8 @@ void WriteProtoList( iterate(ProtoList) { Proto = (PROTOTYPE *) first_node ( ProtoList ); - if (( Proto->Significant && WriteSigProtos ) || - ( ! Proto->Significant && WriteInsigProtos ) ) - WritePrototype( File, N, Proto ); + if ((Proto->Significant && WriteSigProtos) || + (!Proto->Significant && WriteInsigProtos)) + WritePrototype(File, N, Proto); } } diff --git a/classify/clusttool.h b/classify/clusttool.h index e82fa1ef4..a6fe38ea4 100644 --- a/classify/clusttool.h +++ b/classify/clusttool.h @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: clusttool.h - ** Purpose: Definition of clustering utility tools - ** Author: Dan Johnson - ** History: 6/6/89, DSJ, Created. + ** Filename: clusttool.h + ** Purpose: Definition of clustering utility tools + ** Author: Dan Johnson + ** History: 6/6/89, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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 @@ -36,7 +36,7 @@ PROTOSTYLE ReadProtoStyle(FILE *File); FLOAT32 *ReadNFloats (FILE * File, uinT16 N, FLOAT32 Buffer[]); -void WriteParamDesc (FILE * File, uinT16 N, PARAM_DESC ParamDesc[]); +void WriteParamDesc(FILE *File, uinT16 N, const PARAM_DESC ParamDesc[]); void WritePrototype(FILE *File, uinT16 N, PROTOTYPE *Proto); @@ -44,13 +44,9 @@ void WriteNFloats (FILE * File, uinT16 N, FLOAT32 Array[]); void WriteProtoStyle(FILE *File, PROTOSTYLE ProtoStyle); -void WriteProtoList( - FILE *File, - uinT16 N, - PARAM_DESC ParamDesc[], - LIST ProtoList, - BOOL8 WriteSigProtos, - BOOL8 WriteInsigProtos); +void WriteProtoList(FILE *File, uinT16 N, PARAM_DESC ParamDesc[], + LIST ProtoList, BOOL8 WriteSigProtos, + BOOL8 WriteInsigProtos); //--------------Global Data Definitions and Declarations--------------------- // define errors that can be trapped diff --git a/classify/cutoffs.cpp b/classify/cutoffs.cpp index 4f6417149..ffb8692ef 100644 --- a/classify/cutoffs.cpp +++ b/classify/cutoffs.cpp @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: cutoffs.c - ** Purpose: Routines to manipulate an array of class cutoffs. - ** Author: Dan Johnson - ** History: Wed Feb 20 09:28:51 1991, DSJ, Created. + ** Filename: cutoffs.c + ** Purpose: Routines to manipulate an array of class cutoffs. + ** Author: Dan Johnson + ** History: Wed Feb 20 09:28:51 1991, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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/classify/featdefs.cpp b/classify/featdefs.cpp index ad7b79967..dd31f91d8 100644 --- a/classify/featdefs.cpp +++ b/classify/featdefs.cpp @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: featdefs.c - ** Purpose: Definitions of currently defined feature types. - ** Author: Dan Johnson - ** History: Mon May 21 10:26:21 1990, DSJ, Created. + ** Filename: featdefs.c + ** Purpose: Definitions of currently defined feature types. + ** Author: Dan Johnson + ** History: Mon May 21 10:26:21 1990, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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 @@ -289,13 +289,13 @@ CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, * the feature type for the feature with the specified short * name. Trap an error if the specified name is not found. * - * Globals: + * Globals: * - none * * @param FeatureDefs definitions of feature types/extractors * @param ShortName short name of a feature type * @return Feature type which corresponds to ShortName. - * @note Exceptions: + * @note Exceptions: * - ILLEGAL_SHORT_NAME * @note History: Wed May 23 15:36:05 1990, DSJ, Created. */ diff --git a/classify/fpoint.cpp b/classify/fpoint.cpp index 854bea7b7..ff5b7b7cf 100644 --- a/classify/fpoint.cpp +++ b/classify/fpoint.cpp @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: fpoint.c - ** Purpose: Abstract data type for a 2D point (floating point coords) - ** Author: Dan Johnson - ** History: Thu Apr 12 10:44:15 1990, DSJ, Created. + ** Filename: fpoint.c + ** Purpose: Abstract data type for a 2D point (floating point coords) + ** Author: Dan Johnson + ** History: Thu Apr 12 10:44:15 1990, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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 @@ -58,5 +58,4 @@ FLOAT32 NormalizedAngleFrom(FPOINT *Point1, if (Angle < 0.0 || Angle >= FullScale) Angle = 0.0; return (Angle); - } diff --git a/classify/intfx.cpp b/classify/intfx.cpp index 78aa59bbc..9c9870a2e 100644 --- a/classify/intfx.cpp +++ b/classify/intfx.cpp @@ -520,7 +520,7 @@ bool ExtractIntFeat(const TBLOB& blob, tesseract::Classify::ExtractFeatures(blob, nonlinear_norm, &bl_features, &cn_features, results, NULL); - if (bl_features.size() == 0 || cn_features.size() == 0 || + if (bl_features.empty() || cn_features.empty() || bl_features.size() > MAX_NUM_INT_FEATURES || cn_features.size() > MAX_NUM_INT_FEATURES) { return false; // Feature extraction failed. diff --git a/classify/intmatcher.cpp b/classify/intmatcher.cpp index 8fc135ea4..ff999608f 100644 --- a/classify/intmatcher.cpp +++ b/classify/intmatcher.cpp @@ -295,7 +295,8 @@ class ClassPruner { HeapSort(num_classes_, sort_key_, sort_index_); } - /** Prints debug info on the class pruner matches for the pruned classes only. */ + /** Prints debug info on the class pruner matches for the pruned classes only. + */ void DebugMatch(const Classify& classify, const INT_TEMPLATES_STRUCT* int_templates, const INT_FEATURE_STRUCT* features) const { @@ -370,8 +371,9 @@ class ClassPruner { private: /** Array[rounded_classes_] of initial counts for each class. */ int *class_count_; - /// Array[rounded_classes_] of modified counts for each class after normalizing - /// for expected number of features, disabled classes, fragments, and xheights. + /// Array[rounded_classes_] of modified counts for each class after + /// normalizing for expected number of features, disabled classes, fragments, + /// and xheights. int *norm_count_; /** Array[rounded_classes_ +1] of pruned counts that gets sorted */ int *sort_key_; @@ -402,8 +404,9 @@ class ClassPruner { * normalization process (by CLASS_INDEX) * @param expected_num_features Array of expected number of features * for each class (by CLASS_INDEX) - * @param results Sorted Array of pruned classes. Must be an array - * of size at least int_templates->NumClasses. + * @param results Sorted Array of pruned classes. Must be an + * array of size at least + * int_templates->NumClasses. * @param keep_this */ int Classify::PruneClasses(const INT_TEMPLATES_STRUCT* int_templates, @@ -606,7 +609,6 @@ int IntegerMatcher::FindGoodProtos( return NumGoodProtos; } - /** * FindBadFeatures finds all features with maximum feature-evidence < * AdaptFeatureThresh. The list is ordered by increasing feature number. @@ -701,7 +703,6 @@ void IntegerMatcher::Init(tesseract::IntParam *classify_debug_level) { evidence_mult_mask_ = ((1 << kIntEvidenceTruncBits) - 1); } - /*---------------------------------------------------------------------------- Private Code ----------------------------------------------------------------------------*/ @@ -717,8 +718,6 @@ void ScratchEvidence::ClearFeatureEvidence(const INT_CLASS class_template) { class_template->NumConfigs * sizeof(feature_evidence_[0])); } - - /** * Print debugging information for Configuations * @return none @@ -742,7 +741,6 @@ void IMDebugConfiguration(int FeatureNum, cprintf ("\n"); } - /** * Print debugging information for Configuations * @return none @@ -795,10 +793,10 @@ int IntegerMatcher::UpdateTablesForFeature( uinT32 XFeatureAddress; uinT32 YFeatureAddress; uinT32 ThetaFeatureAddress; - uinT8 *UINT8Pointer; + uinT8* UINT8Pointer; int ProtoIndex; uinT8 Temp; - int *IntPointer; + int* IntPointer; int ConfigNum; inT32 M3; inT32 A3; @@ -916,7 +914,6 @@ int IntegerMatcher::UpdateTablesForFeature( return SumOverConfigs; } - /** * Print debugging information for Configuations * @return none @@ -1165,8 +1162,6 @@ void ScratchEvidence::UpdateSumOfProtoEvidences( } } - - /** * Normalize Sum of Proto and Feature Evidence by dividing by the sum of * the Feature Lengths and the Proto Lengths for each configuration. @@ -1180,7 +1175,6 @@ void ScratchEvidence::NormalizeSums( } } - /** * Find the best match for the current class and update the Result * with the configuration and match rating. diff --git a/classify/intmatcher.h b/classify/intmatcher.h index 46dbfc5a8..df678d75e 100644 --- a/classify/intmatcher.h +++ b/classify/intmatcher.h @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: intmatcher.h - ** Purpose: Interface to high level generic classifier routines. - ** Author: Robert Moss - ** History: Wed Feb 13 15:24:15 MST 1991, RWM, Created. + ** Filename: intmatcher.h + ** Purpose: Interface to high level generic classifier routines. + ** Author: Robert Moss + ** History: Wed Feb 13 15:24:15 MST 1991, RWM, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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/classify/intproto.cpp b/classify/intproto.cpp index 4c2f0d953..473ffd96d 100644 --- a/classify/intproto.cpp +++ b/classify/intproto.cpp @@ -46,9 +46,7 @@ #include "config_auto.h" #endif -using tesseract::FontInfo; using tesseract::FontSet; -using tesseract::FontSpacingInfo; /* match debug display constants*/ #define PROTO_PRUNER_SCALE (4.0) @@ -326,10 +324,8 @@ int AddIntProto(INT_CLASS Class) { Word < Proto->Configs + WERDS_PER_CONFIG_VEC; *Word++ = 0); return (Index); - } - /** * This routine adds Proto to the class pruning tables * for the specified class in Templates. @@ -372,7 +368,6 @@ void AddProtoToClassPruner (PROTO Proto, CLASS_ID ClassId, } } /* AddProtoToClassPruner */ - /** * This routine updates the proto pruner lookup tables * for Class to include a new proto identified by ProtoId @@ -432,7 +427,6 @@ void AddProtoToProtoPruner(PROTO Proto, int ProtoId, FillPPLinearBits(ProtoSet->ProtoPruner[PRUNER_Y], Index, Y, Pad, debug); } /* AddProtoToProtoPruner */ - /** * Returns a quantized bucket for the given param shifted by offset, * notionally (param + offset) * num_buckets, but clipped and casted to the @@ -550,7 +544,6 @@ void Classify::ConvertProto(PROTO Proto, int ProtoId, INT_CLASS Class) { P->A, P->B, P->C, Class->ProtoLengths[ProtoId]); } /* ConvertProto */ - /** * This routine converts from the old floating point format * to the new integer format. @@ -627,7 +620,7 @@ INT_TEMPLATES Classify::CreateIntTemplates(CLASSES FloatProtos, * @note Exceptions: none * @note History: Thu Mar 21 14:45:04 1991, DSJ, Created. */ -void DisplayIntFeature(const INT_FEATURE_STRUCT* Feature, FLOAT32 Evidence) { +void DisplayIntFeature(const INT_FEATURE_STRUCT *Feature, FLOAT32 Evidence) { ScrollView::Color color = GetMatchColorFor(Evidence); RenderIntFeature(IntMatchWindow, Feature, color); if (FeatureDisplayWindow) { @@ -635,7 +628,6 @@ void DisplayIntFeature(const INT_FEATURE_STRUCT* Feature, FLOAT32 Evidence) { } } /* DisplayIntFeature */ - /** * This routine renders the specified proto into a * global display list. @@ -720,7 +712,6 @@ void free_int_class(INT_CLASS int_class) { Efree(int_class); } - /** * This routine allocates a new set of integer templates * initialized to hold 0 classes. @@ -1218,7 +1209,6 @@ FLOAT32 BucketStart(int Bucket, FLOAT32 Offset, int NumBuckets) { } /* BucketStart */ - /** * This routine returns the parameter value which * corresponds to the end of the specified bucket. @@ -1236,7 +1226,6 @@ FLOAT32 BucketEnd(int Bucket, FLOAT32 Offset, int NumBuckets) { return (((FLOAT32) (Bucket + 1) / NumBuckets) - Offset); } /* BucketEnd */ - /** * This routine fills in the section of a class pruner * corresponding to a single x value for a single proto of @@ -1284,7 +1273,6 @@ void DoFill(FILL_SPEC *FillSpec, } } /* DoFill */ - /** * Return TRUE if the specified table filler is done, i.e. * if it has no more lines to fill. @@ -1306,7 +1294,6 @@ BOOL8 FillerDone(TABLE_FILLER *Filler) { } /* FillerDone */ - /** * This routine sets Bit in each bit vector whose * bucket lies within the range Center +- Spread. The fill @@ -1349,7 +1336,6 @@ void FillPPCircularBits(uinT32 ParamTable[NUM_PP_BUCKETS][WERDS_PER_PP_VECTOR], } /* FillPPCircularBits */ - /** * This routine sets Bit in each bit vector whose * bucket lies within the range Center +- Spread. The fill @@ -1516,7 +1502,6 @@ void GetCPPadsForLevel(int Level, } /* GetCPPadsForLevel */ - /** * @param Evidence evidence value to return color for * @return Color which corresponds to specified Evidence value. @@ -1538,7 +1523,6 @@ ScrollView::Color GetMatchColorFor(FLOAT32 Evidence) { return ScrollView::BLUE; } /* GetMatchColorFor */ - /** * This routine returns (in Fill) the specification of * the next line to be filled from Filler. FillerDone() should @@ -1589,7 +1573,6 @@ void GetNextFill(TABLE_FILLER *Filler, FILL_SPEC *Fill) { } /* GetNextFill */ - /** * This routine computes a data structure (Filler) * which can be used to fill in a rectangle surrounding @@ -1723,8 +1706,10 @@ void InitTableFiller (FLOAT32 EndPad, FLOAT32 SidePad, /* translate into bucket positions and deltas */ Filler->X = Bucket8For(Start.x, XS, NB); - Filler->StartDelta = -(inT16) ((Sin / Cos) * 256); - Filler->EndDelta = (inT16) ((Cos / Sin) * 256); + Filler->StartDelta = static_cast(ClipToRange( + -IntCastRounded((Sin / Cos) * 256), MIN_INT16, MAX_INT16)); + Filler->EndDelta = static_cast(ClipToRange( + IntCastRounded((Cos / Sin) * 256), MIN_INT16, MAX_INT16)); XAdjust = BucketEnd(Filler->X, XS, NB) - Start.x; YAdjust = XAdjust * Sin / Cos; @@ -1787,7 +1772,6 @@ void RenderIntFeature(ScrollView *window, const INT_FEATURE_STRUCT* Feature, window->DrawTo(X + Dx, Y + Dy); } /* RenderIntFeature */ - /** * This routine extracts the parameters of the specified * proto from the class description and adds a rendering of diff --git a/classify/intproto.h b/classify/intproto.h index d2c07147a..262974b83 100644 --- a/classify/intproto.h +++ b/classify/intproto.h @@ -52,7 +52,7 @@ class FCOORD; #define NUM_CP_BUCKETS 24 #define CLASSES_PER_CP 32 #define NUM_BITS_PER_CLASS 2 -#define CLASS_PRUNER_CLASS_MASK (~(~0 << NUM_BITS_PER_CLASS)) +#define CLASS_PRUNER_CLASS_MASK (~(~0u << NUM_BITS_PER_CLASS)) #define CLASSES_PER_CP_WERD (CLASSES_PER_CP / NUM_BITS_PER_CLASS) #define PROTOS_PER_PP_WERD BITS_PER_WERD #define BITS_PER_CP_VECTOR (CLASSES_PER_CP * NUM_BITS_PER_CLASS) diff --git a/classify/kdtree.cpp b/classify/kdtree.cpp index 61a94f66c..6ba7086d2 100644 --- a/classify/kdtree.cpp +++ b/classify/kdtree.cpp @@ -70,11 +70,11 @@ class MinK { const Element* elements() { return elements_; } private: - const Key max_key_; //< the maximum possible Key - Element* elements_; //< unsorted array of elements + const Key max_key_; //< the maximum possible Key + Element *elements_; //< unsorted array of elements int elements_count_; //< the number of results collected so far - int k_; //< the number of results we want from the search - int max_index_; //< the index of the result with the largest key + int k_; //< the number of results we want from the search + int max_index_; //< the index of the result with the largest key }; template @@ -117,7 +117,8 @@ bool MinK::insert(Key key, Value value) { //----------------------------------------------------------------------------- -/** Helper class for searching for the k closest points to query_point in tree. */ +/** Helper class for searching for the k closest points to query_point in tree. + */ class KDTreeSearch { public: KDTreeSearch(KDTREE* tree, FLOAT32 *query_point, int k_closest); @@ -241,14 +242,13 @@ void KDStore(KDTREE *Tree, FLOAT32 *Key, void *Data) { *PtrToNode = MakeKDNode(Tree, Key, (void *) Data, Level); } /* KDStore */ - /** - * This routine deletes a node from Tree. The node to be - * deleted is specified by the Key for the node and the Data - * contents of the node. These two pointers must be identical - * to the pointers that were used for the node when it was - * originally stored in the tree. A node will be deleted from - * the tree only if its key and data pointers are identical + * This routine deletes a node from Tree. The node to be + * deleted is specified by the Key for the node and the Data + * contents of the node. These two pointers must be identical + * to the pointers that were used for the node when it was + * originally stored in the tree. A node will be deleted from + * the tree only if its key and data pointers are identical * to Key and Data respectively. The tree is re-formed by removing * the affected subtree and inserting all elements but the root. * @@ -298,7 +298,6 @@ KDDelete (KDTREE * Tree, FLOAT32 Key[], void *Data) { } } /* KDDelete */ - /** * This routine searches the K-D tree specified by Tree and * finds the QuerySize nearest neighbors of Query. All neighbors @@ -442,7 +441,7 @@ void KDTreeSearch::SearchRec(int level, KDNODE *sub_tree) { /*---------------------------------------------------------------------------*/ -/** +/** *Returns the Euclidean distance squared between p1 and p2 for all essential * dimensions. * @param k keys are in k-space @@ -541,7 +540,6 @@ void Walk(KDTREE *tree, void_proc action, void *context, Walk(tree, action, context, sub_tree->Right, NextLevel(tree, level)); } - /** Given a subtree nodes, insert all of its elements into tree. */ void InsertNodes(KDTREE *tree, KDNODE *nodes) { if (nodes == NULL) diff --git a/classify/mastertrainer.cpp b/classify/mastertrainer.cpp index 866a617a2..cd7e93b9f 100644 --- a/classify/mastertrainer.cpp +++ b/classify/mastertrainer.cpp @@ -362,9 +362,11 @@ bool MasterTrainer::LoadFontInfo(const char* filename) { fontinfo.name = font_name; fontinfo.properties = 0; fontinfo.universal_id = 0; - if (tfscanf(fp, "%1024s %i %i %i %i %i\n", font_name, - &italic, &bold, &fixed, &serif, &fraktur) != 6) + if (tfscanf(fp, "%1024s %i %i %i %i %i\n", font_name, &italic, &bold, + &fixed, &serif, &fraktur) != 6) { + delete[] font_name; continue; + } fontinfo.properties = (italic << 0) + (bold << 1) + @@ -373,6 +375,8 @@ bool MasterTrainer::LoadFontInfo(const char* filename) { (fraktur << 4); if (!fontinfo_table_.contains(fontinfo)) { fontinfo_table_.push_back(fontinfo); + } else { + delete[] font_name; } } fclose(fp); diff --git a/classify/mf.cpp b/classify/mf.cpp index d0c59487e..37cd2eca0 100644 --- a/classify/mf.cpp +++ b/classify/mf.cpp @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: mf.c - ** Purpose: Micro-feature interface to flexible feature extractor. - ** Author: Dan Johnson - ** History: Thu May 24 09:08:38 1990, DSJ, Created. + ** Filename: mf.c + ** Purpose: Micro-feature interface to flexible feature extractor. + ** Author: Dan Johnson + ** History: Thu May 24 09:08:38 1990, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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 @@ -36,7 +36,7 @@ * Call the old micro-feature extractor and then copy * the features into the new format. Then deallocate the * old micro-features. - * @param Blob blob to extract micro-features from + * @param Blob blob to extract micro-features from * @param cn_denorm control parameter to feature extractor. * @return Micro-features for Blob. * @note Exceptions: none diff --git a/classify/mfdefs.cpp b/classify/mfdefs.cpp index abe8d0c71..0f225e8b0 100644 --- a/classify/mfdefs.cpp +++ b/classify/mfdefs.cpp @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: mfdefs.c - ** Purpose: Basic routines for manipulating micro-features - ** Author: Dan Johnson - ** History: Mon Jan 22 08:48:58 1990, DSJ, Created. + ** Filename: mfdefs.c + ** Purpose: Basic routines for manipulating micro-features + ** Author: Dan Johnson + ** History: Mon Jan 22 08:48:58 1990, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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 @@ -32,7 +32,7 @@ * @return New MICROFEATURE * @note History: 7/27/89, DSJ, Created. */ -MICROFEATURE NewMicroFeature() { +MICROFEATURE NewMicroFeature() { return ((MICROFEATURE) Emalloc (sizeof (MFBLOCK))); } /* NewMicroFeature */ @@ -41,10 +41,10 @@ MICROFEATURE NewMicroFeature() { /** * This routine deallocates all of the memory consumed by * a list of micro-features. - * @param MicroFeatures list of micro-features to be freed + * @param MicroFeatures list of micro-features to be freed * @return none * @note History: 7/27/89, DSJ, Created. */ -void FreeMicroFeatures(MICROFEATURES MicroFeatures) { +void FreeMicroFeatures(MICROFEATURES MicroFeatures) { destroy_nodes(MicroFeatures, Efree); } /* FreeMicroFeatures */ diff --git a/classify/mfoutline.cpp b/classify/mfoutline.cpp index 511c34d41..59593a852 100644 --- a/classify/mfoutline.cpp +++ b/classify/mfoutline.cpp @@ -35,7 +35,8 @@ ----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -/** Convert a blob into a list of MFOUTLINEs (float-based microfeature format). */ +/** Convert a blob into a list of MFOUTLINEs (float-based microfeature format). + */ LIST ConvertBlob(TBLOB *blob) { LIST outlines = NIL_LIST; return (blob == NULL) @@ -344,7 +345,6 @@ void ChangeDirection(MFOUTLINE Start, MFOUTLINE End, DIRECTION Direction) { } /* ChangeDirection */ - /** * This routine normalizes each point in Outline by * translating it to the specified center and scaling it @@ -378,7 +378,6 @@ void CharNormalizeOutline(MFOUTLINE Outline, const DENORM& cn_denorm) { } /* CharNormalizeOutline */ - /** * This routine computes the slope from Start to Finish and * and then computes the approximate direction of the line diff --git a/classify/mfx.cpp b/classify/mfx.cpp index 3da4fb3d0..6fd8ed5da 100644 --- a/classify/mfx.cpp +++ b/classify/mfx.cpp @@ -128,7 +128,6 @@ FLOAT32 ComputeOrientation(MFEDGEPT *Start, MFEDGEPT *End) { return (Orientation); } /* ComputeOrientation */ - /** * Convert Outline to MicroFeatures * @param Outline outline to extract micro-features from @@ -164,7 +163,6 @@ MICROFEATURES ConvertToMicroFeatures(MFOUTLINE Outline, return (MicroFeatures); } /* ConvertToMicroFeatures */ - /** * This routine computes the feature parameters which describe * the micro-feature that starts and Start and ends at End. @@ -178,7 +176,7 @@ MICROFEATURES ConvertToMicroFeatures(MFOUTLINE Outline, * @return New micro-feature or NULL if the feature was rejected. * @note Globals: none * @note Exceptions: none - * @note History: + * @note History: * - 7/26/89, DSJ, Created. * - 11/17/89, DSJ, Added handling for Start and End same point. */ diff --git a/classify/mfx.h b/classify/mfx.h index 05ce29cee..5ed006dcc 100644 --- a/classify/mfx.h +++ b/classify/mfx.h @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: mfx.h - ** Purpose: Definition of micro-feature extraction routines - ** Author: Dan Johnson - ** History: 5/29/89, DSJ, Created. + ** Filename: mfx.h + ** Purpose: Definition of micro-feature extraction routines + ** Author: Dan Johnson + ** History: 5/29/89, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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/classify/normfeat.cpp b/classify/normfeat.cpp index a4ac672a1..f297b3b05 100644 --- a/classify/normfeat.cpp +++ b/classify/normfeat.cpp @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: normfeat.c - ** Purpose: Definition of char normalization features. - ** Author: Dan Johnson - ** History: 12/14/90, DSJ, Created. + ** Filename: normfeat.c + ** Purpose: Definition of char normalization features. + ** Author: Dan Johnson + ** History: 12/14/90, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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 @@ -33,7 +33,6 @@ FLOAT32 ActualOutlineLength(FEATURE Feature) { return (Feature->Params[CharNormLength] * LENGTH_COMPRESSION); } - /** * Return the character normalization feature for a blob. * diff --git a/classify/normmatch.cpp b/classify/normmatch.cpp index 488cd1652..b0e066a89 100644 --- a/classify/normmatch.cpp +++ b/classify/normmatch.cpp @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: normmatch.c - ** Purpose: Simple matcher based on character normalization features. - ** Author: Dan Johnson - ** History: Wed Dec 19 16:18:06 1990, DSJ, Created. + ** Filename: normmatch.c + ** Purpose: Simple matcher based on character normalization features. + ** Author: Dan Johnson + ** History: Wed Dec 19 16:18:06 1990, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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 @@ -197,10 +197,10 @@ double NormEvidenceOf(register double NormAdj) { /*---------------------------------------------------------------------------*/ /** * This routine dumps out detailed normalization match info. - * @param File open text file to dump match debug info to - * @param NumParams # of parameters in proto and feature - * @param Proto[] array of prototype parameters - * @param Feature[] array of feature parameters + * @param File open text file to dump match debug info to + * @param NumParams # of parameters in proto and feature + * @param Proto[] array of prototype parameters + * @param Feature[] array of feature parameters * Globals: none * @return none * @note Exceptions: none diff --git a/classify/ocrfeatures.cpp b/classify/ocrfeatures.cpp index 0895ed088..7df813504 100644 --- a/classify/ocrfeatures.cpp +++ b/classify/ocrfeatures.cpp @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: features.c - ** Purpose: Generic definition of a feature. - ** Author: Dan Johnson - ** History: Mon May 21 10:49:04 1990, DSJ, Created. + ** Filename: features.c + ** Purpose: Generic definition of a feature. + ** Author: Dan Johnson + ** History: Mon May 21 10:49:04 1990, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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 @@ -66,12 +66,11 @@ void FreeFeature(FEATURE Feature) { } /* FreeFeature */ - /** * Release the memory consumed by the specified feature * set. This routine also frees the memory consumed by the * features contained in the set. - * @param FeatureSet set of features to be freed + * @param FeatureSet set of features to be freed * @return none * @note History: Mon May 21 13:59:46 1990, DSJ, Created. */ @@ -85,11 +84,10 @@ void FreeFeatureSet(FEATURE_SET FeatureSet) { } } /* FreeFeatureSet */ - /** * Allocate and return a new feature of the specified * type. - * @param FeatureDesc description of feature to be created. + * @param FeatureDesc description of feature to be created. * @return New #FEATURE. * @note History: Mon May 21 14:06:42 1990, DSJ, Created. */ @@ -105,11 +103,10 @@ FEATURE NewFeature(const FEATURE_DESC_STRUCT* FeatureDesc) { } /* NewFeature */ - /** * Allocate and return a new feature set large enough to * hold the specified number of features. - * @param NumFeatures maximum # of features to be put in feature set + * @param NumFeatures maximum # of features to be put in feature set * @return New #FEATURE_SET. * @note History: Mon May 21 14:22:40 1990, DSJ, Created. */ @@ -124,7 +121,6 @@ FEATURE_SET NewFeatureSet(int NumFeatures) { } /* NewFeatureSet */ - /** * Create a new feature of the specified type and read in * the value of its parameters from File. The extra penalty @@ -135,10 +131,11 @@ FEATURE_SET NewFeatureSet(int NumFeatures) { * @param File open text file to read feature from * @param FeatureDesc specifies type of feature to read from File * @return New #FEATURE read from File. - * @note Exceptions: #ILLEGAL_FEATURE_PARAM if text file doesn't match expected format + * @note Exceptions: #ILLEGAL_FEATURE_PARAM if text file doesn't match expected + * format * @note History: Wed May 23 08:53:16 1990, DSJ, Created. */ -FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) { +FEATURE ReadFeature(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) { FEATURE Feature; int i; @@ -153,7 +150,6 @@ FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) { return (Feature); } /* ReadFeature */ - /** * Create a new feature set of the specified type and read in * the features from File. The correct text representation @@ -165,7 +161,7 @@ FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) { * @return New feature set read from File. * @note History: Wed May 23 09:17:31 1990, DSJ, Created. */ -FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) { +FEATURE_SET ReadFeatureSet(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) { FEATURE_SET FeatureSet; int NumFeatures; int i; @@ -180,7 +176,6 @@ FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) { return (FeatureSet); } /* ReadFeatureSet */ - /** * Appends a textual representation of Feature to str. * This representation is simply a list of the N parameters @@ -203,7 +198,6 @@ void WriteFeature(FEATURE Feature, STRING* str) { *str += "\n"; } /* WriteFeature */ - /** * Write a textual representation of FeatureSet to File. * This representation is an integer specifying the number of @@ -224,7 +218,6 @@ void WriteFeatureSet(FEATURE_SET FeatureSet, STRING* str) { } } /* WriteFeatureSet */ - /** * Write a textual representation of FeatureDesc to File * in the old format (i.e. the format used by the clusterer). @@ -240,7 +233,7 @@ void WriteFeatureSet(FEATURE_SET FeatureSet, STRING* str) { * @return none * @note History: Fri May 25 15:27:18 1990, DSJ, Created. */ -void WriteOldParamDesc(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) { +void WriteOldParamDesc(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) { int i; fprintf (File, "%d\n", FeatureDesc->NumParams); diff --git a/classify/outfeat.cpp b/classify/outfeat.cpp index b1a4a9be9..76597f7c1 100644 --- a/classify/outfeat.cpp +++ b/classify/outfeat.cpp @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: outfeat.c - ** Purpose: Definition of outline-features. - ** Author: Dan Johnson - ** History: 11/13/90, DSJ, Created. + ** Filename: outfeat.c + ** Purpose: Definition of outline-features. + ** Author: Dan Johnson + ** History: 11/13/90, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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 @@ -40,7 +40,7 @@ namespace tesseract { * @return Outline-features for Blob. * @note Globals: none * @note Exceptions: none - * @note History: + * @note History: * - 11/13/90, DSJ, Created. * - 05/24/91, DSJ, Updated for either char or baseline normalize. */ @@ -115,7 +115,7 @@ void AddOutlineFeatureToSet(FPOINT *Start, * @return none (results are returned in FeatureSet) * @note Globals: none * @note Exceptions: none - * @note History: + * @note History: * - 11/13/90, DSJ, Created. * - 5/24/91, DSJ, Added hidden edge capability. */ diff --git a/classify/picofeat.cpp b/classify/picofeat.cpp index 74beb18f3..a4a39263c 100644 --- a/classify/picofeat.cpp +++ b/classify/picofeat.cpp @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: picofeat.c - ** Purpose: Definition of pico-features. - ** Author: Dan Johnson - ** History: 9/4/90, DSJ, Created. + ** Filename: picofeat.c + ** Purpose: Definition of pico-features. + ** Author: Dan Johnson + ** History: 9/4/90, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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 @@ -98,7 +98,7 @@ FEATURE_SET Classify::ExtractPicoFeatures(TBLOB *Blob) { * nearest whole number of pico-features. The pico-features * are spaced evenly over the entire segment. * Globals: - * - classify_pico_feature_length length of a single pico-feature + * - classify_pico_feature_length length of a single pico-feature * @param Start starting point of pico-feature * @param End ending point of pico-feature * @param FeatureSet set to add pico-feature to diff --git a/classify/shapeclassifier.cpp b/classify/shapeclassifier.cpp index a39c8a24b..e0ee3373d 100644 --- a/classify/shapeclassifier.cpp +++ b/classify/shapeclassifier.cpp @@ -176,7 +176,7 @@ void ShapeClassifier::UnicharPrintResults( for (int i = 0; i < results.size(); ++i) { tprintf("%g: c_id=%d=%s", results[i].rating, results[i].unichar_id, GetUnicharset().id_to_unichar(results[i].unichar_id)); - if (results[i].fonts.size() != 0) { + if (!results[i].fonts.empty()) { tprintf(" Font Vector:"); for (int f = 0; f < results[i].fonts.size(); ++f) { tprintf(" %d", results[i].fonts[f].fontinfo_id); diff --git a/cube/cube_search_object.cpp b/cube/cube_search_object.cpp index 61294f26b..731dd3527 100644 --- a/cube/cube_search_object.cpp +++ b/cube/cube_search_object.cpp @@ -400,7 +400,7 @@ bool CubeSearchObject::ComputeSpaceCosts() { float prob = 0.0; // gap is too small => no space - if (gap < min_spc_gap_) { + if (gap < min_spc_gap_ || max_spc_gap_ == min_spc_gap_) { prob = 0.0; } else if (gap > max_spc_gap_) { // gap is too big => definite space diff --git a/cube/hybrid_neural_net_classifier.cpp b/cube/hybrid_neural_net_classifier.cpp index 671a74acd..9aa3026d8 100644 --- a/cube/hybrid_neural_net_classifier.cpp +++ b/cube/hybrid_neural_net_classifier.cpp @@ -330,7 +330,7 @@ bool HybridNeuralNetCharClassifier::LoadNets(const string &data_file_path, // split into lines vector str_vec; CubeUtils::SplitStringUsing(str, "\r\n", &str_vec); - if (str_vec.size() <= 0) { + if (str_vec.empty()) { return false; } diff --git a/cube/word_unigrams.cpp b/cube/word_unigrams.cpp index 70cc9ee1e..b92289d8e 100644 --- a/cube/word_unigrams.cpp +++ b/cube/word_unigrams.cpp @@ -163,7 +163,7 @@ int WordUnigrams::Cost(const char_32 *key_str32, CubeUtils::SplitStringUsing(key_str, " \t", &words); // no words => no cost - if (words.size() <= 0) { + if (words.empty()) { return 0; } diff --git a/cutil/bitvec.h b/cutil/bitvec.h index f70d748b9..d2a364d26 100644 --- a/cutil/bitvec.h +++ b/cutil/bitvec.h @@ -30,29 +30,29 @@ typedef uinT32 *BIT_VECTOR; /*----------------------------------------------------------------------------- Public Function Prototypes -----------------------------------------------------------------------------*/ -#define zero_all_bits(array,length) \ -{\ - int index; /*temporary index*/\ -\ -for (index=0;indexparams()), STRING_INIT_MEMBER(user_words_suffix, "", "A suffix of user-provided words located in tessdata.", @@ -54,33 +53,41 @@ Dict::Dict(CCUtil* ccutil) getCCUtil()->params()), BOOL_INIT_MEMBER(load_unambig_dawg, true, "Load unambiguous word dawg.", getCCUtil()->params()), - BOOL_INIT_MEMBER(load_punc_dawg, true, "Load dawg with punctuation" - " patterns.", getCCUtil()->params()), - BOOL_INIT_MEMBER(load_number_dawg, true, "Load dawg with number" - " patterns.", getCCUtil()->params()), - BOOL_INIT_MEMBER(load_bigram_dawg, true, "Load dawg with special word " - "bigrams.", getCCUtil()->params()), + BOOL_INIT_MEMBER(load_punc_dawg, true, + "Load dawg with punctuation" + " patterns.", + getCCUtil()->params()), + BOOL_INIT_MEMBER(load_number_dawg, true, + "Load dawg with number" + " patterns.", + getCCUtil()->params()), + BOOL_INIT_MEMBER(load_bigram_dawg, true, + "Load dawg with special word " + "bigrams.", + getCCUtil()->params()), double_MEMBER(xheight_penalty_subscripts, 0.125, "Score penalty (0.1 = 10%) added if there are subscripts " "or superscripts in a word, but it is otherwise OK.", getCCUtil()->params()), double_MEMBER(xheight_penalty_inconsistent, 0.25, "Score penalty (0.1 = 10%) added if an xheight is " - "inconsistent.", getCCUtil()->params()), + "inconsistent.", + getCCUtil()->params()), double_MEMBER(segment_penalty_dict_frequent_word, 1.0, "Score multiplier for word matches which have good case and" "are frequent in the given language (lower is better).", getCCUtil()->params()), double_MEMBER(segment_penalty_dict_case_ok, 1.1, "Score multiplier for word matches that have good case " - "(lower is better).", getCCUtil()->params()), + "(lower is better).", + getCCUtil()->params()), double_MEMBER(segment_penalty_dict_case_bad, 1.3125, "Default score multiplier for word matches, which may have " "case issues (lower is better).", getCCUtil()->params()), double_MEMBER(segment_penalty_ngram_best_choice, 1.24, - "Multipler to for the best choice from the ngram model.", - getCCUtil()->params()), + "Multipler to for the best choice from the ngram model.", + getCCUtil()->params()), double_MEMBER(segment_penalty_dict_nonword, 1.25, "Score multiplier for glyph fragment segmentations which " "do not match a dictionary word (lower is better).", @@ -88,11 +95,13 @@ Dict::Dict(CCUtil* ccutil) double_MEMBER(segment_penalty_garbage, 1.50, "Score multiplier for poorly cased strings that are not in" " the dictionary and generally look like garbage (lower is" - " better).", getCCUtil()->params()), + " better).", + getCCUtil()->params()), STRING_MEMBER(output_ambig_words_file, "", "Output file for ambiguities found in the dictionary", getCCUtil()->params()), - INT_MEMBER(dawg_debug_level, 0, "Set to 1 for general debug info" + INT_MEMBER(dawg_debug_level, 0, + "Set to 1 for general debug info" ", to 2 for more details, to 3 to see all the debug messages", getCCUtil()->params()), INT_MEMBER(hyphen_debug_level, 0, "Debug level for hyphenated words.", @@ -109,12 +118,12 @@ Dict::Dict(CCUtil* ccutil) "Certainty threshold for non-dict words", getCCUtil()->params()), double_MEMBER(stopper_phase2_certainty_rejection_offset, 1.0, - "Reject certainty offset", - getCCUtil()->params()), + "Reject certainty offset", getCCUtil()->params()), INT_MEMBER(stopper_smallword_size, 2, "Size of dict word to be treated as non-dict word", getCCUtil()->params()), - double_MEMBER(stopper_certainty_per_char, -0.50, "Certainty to add" + double_MEMBER(stopper_certainty_per_char, -0.50, + "Certainty to add" " for each dict char above small word size.", getCCUtil()->params()), double_MEMBER(stopper_allowable_character_badness, 3.0, @@ -130,9 +139,9 @@ Dict::Dict(CCUtil* ccutil) "Deprecated- backward compatibility only", getCCUtil()->params()), INT_MEMBER(tessedit_truncate_wordchoice_log, 10, - "Max words to keep in list", - getCCUtil()->params()), - STRING_MEMBER(word_to_debug, "", "Word for which stopper debug" + "Max words to keep in list", getCCUtil()->params()), + STRING_MEMBER(word_to_debug, "", + "Word for which stopper debug" " information should be printed to stdout", getCCUtil()->params()), STRING_MEMBER(word_to_debug_lengths, "", @@ -141,10 +150,10 @@ Dict::Dict(CCUtil* ccutil) INT_MEMBER(fragments_debug, 0, "Debug character fragments", getCCUtil()->params()), BOOL_MEMBER(segment_nonalphabetic_script, false, - "Don't use any alphabetic-specific tricks." - "Set to true in the traineddata config file for" - " scripts that are cursive or inherently fixed-pitch", - getCCUtil()->params()), + "Don't use any alphabetic-specific tricks." + "Set to true in the traineddata config file for" + " scripts that are cursive or inherently fixed-pitch", + getCCUtil()->params()), BOOL_MEMBER(save_doc_words, 0, "Save Document Words", getCCUtil()->params()), double_MEMBER(doc_dict_pending_threshold, 0.0, @@ -152,8 +161,10 @@ Dict::Dict(CCUtil* ccutil) getCCUtil()->params()), double_MEMBER(doc_dict_certainty_threshold, -2.25, "Worst certainty for words that can be inserted into the" - "document dictionary", getCCUtil()->params()), - INT_MEMBER(max_permuter_attempts, 10000, "Maximum number of different" + "document dictionary", + getCCUtil()->params()), + INT_MEMBER(max_permuter_attempts, 10000, + "Maximum number of different" " character choices to consider during permutation." " This limit is especially useful when user patterns" " are specified, since overly generic patterns can result in" @@ -179,6 +190,7 @@ Dict::Dict(CCUtil* ccutil) } Dict::~Dict() { + End(); if (hyphen_word_ != NULL) delete hyphen_word_; if (output_ambig_words_file_ != NULL) fclose(output_ambig_words_file_); } diff --git a/dict/stopper.cpp b/dict/stopper.cpp index 660b4c8cf..a0e3c02b2 100644 --- a/dict/stopper.cpp +++ b/dict/stopper.cpp @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: stopper.c - ** Purpose: Stopping criteria for word classifier. - ** Author: Dan Johnson - ** History: Mon Apr 29 14:56:49 1991, DSJ, Created. + ** Filename: stopper.c + ** Purpose: Stopping criteria for word classifier. + ** Author: Dan Johnson + ** History: Mon Apr 29 14:56:49 1991, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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 @@ -41,7 +41,6 @@ #pragma warning(disable:4800) // int/bool warnings #endif -using tesseract::ScriptPos; /*---------------------------------------------------------------------------- Private Code ----------------------------------------------------------------------------*/ diff --git a/dict/stopper.h b/dict/stopper.h index b028b0ee2..58d23734d 100644 --- a/dict/stopper.h +++ b/dict/stopper.h @@ -1,10 +1,10 @@ /****************************************************************************** - ** Filename: stopper.h - ** Purpose: Stopping criteria for word classifier. - ** Author: Dan Johnson - ** History: Wed May 1 09:42:57 1991, DSJ, Created. + ** Filename: stopper.h + ** Purpose: Stopping criteria for word classifier. + ** Author: Dan Johnson + ** History: Wed May 1 09:42:57 1991, DSJ, Created. ** - ** (c) Copyright Hewlett-Packard Company, 1988. + ** (c) Copyright Hewlett-Packard Company, 1988. ** 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/opencl/oclkernels.h b/opencl/oclkernels.h index b3a8316b1..66fb0527d 100644 --- a/opencl/oclkernels.h +++ b/opencl/oclkernels.h @@ -1092,7 +1092,8 @@ void kernel_ThresholdRectToPix( for ( int c = 0; c < NUM_CHANNELS; c++) { unsigned char pixChan = pixels.s[p*NUM_CHANNELS + c]; if (pHi_Values[c] >= 0 && (pixChan > pThresholds[c]) == (pHi_Values[c] == 0)) { - word |= (((uint)0x80000000) >> ((b*PIXELS_PER_BURST+p)&31)); + const uint kTopBit = 0x80000000; + word |= (kTopBit >> ((b*PIXELS_PER_BURST+p)&31)); } } } @@ -1157,7 +1158,8 @@ void kernel_ThresholdRectToPix_OneChan( \n#endif\n unsigned char pixChan = pixels.s[idx]; if (pHi_Values[0] >= 0 && (pixChan > pThresholds[0]) == (pHi_Values[0] == 0)) { - word |= (0x80000000 >> ((b*PIXELS_PER_BURST+p)&31)); + const uint kTopBit = 0x80000000; + word |= (kTopBit >> ((b*PIXELS_PER_BURST+p)&31)); } } } diff --git a/opencl/opencl_device_selection.h b/opencl/opencl_device_selection.h index 74272b35c..305151ee5 100644 --- a/opencl/opencl_device_selection.h +++ b/opencl/opencl_device_selection.h @@ -63,8 +63,10 @@ static ds_status releaseDSProfile(ds_profile* profile, ds_score_release sr) { if (profile->devices!=NULL && sr!=NULL) { unsigned int i; for (i = 0; i < profile->numDevices; i++) { - if (profile->devices[i].oclDeviceName) free(profile->devices[i].oclDeviceName); - if (profile->devices[i].oclDriverVersion) free(profile->devices[i].oclDriverVersion); + if (profile->devices[i].oclDeviceName) + free(profile->devices[i].oclDeviceName); + if (profile->devices[i].oclDriverVersion) + free(profile->devices[i].oclDriverVersion); status = sr(profile->devices[i].score); if (status != DS_SUCCESS) break; diff --git a/opencl/openclwrapper.cpp b/opencl/openclwrapper.cpp index f35fcd439..20070fa05 100644 --- a/opencl/openclwrapper.cpp +++ b/opencl/openclwrapper.cpp @@ -1,6 +1,6 @@ #ifdef _WIN32 -#include #include +#include #else #include @@ -16,23 +16,24 @@ #include "thresholder.h" #if ON_APPLE -#include #include +#include #endif /* Convenience macro to test the version of Leptonica. */ #if defined(LIBLEPT_MAJOR_VERSION) && defined(LIBLEPT_MINOR_VERSION) -# define TESSERACT_LIBLEPT_PREREQ(maj, min) \ - ((LIBLEPT_MAJOR_VERSION) > (maj) || ((LIBLEPT_MAJOR_VERSION) == (maj) && (LIBLEPT_MINOR_VERSION) >= (min))) +#define TESSERACT_LIBLEPT_PREREQ(maj, min) \ + ((LIBLEPT_MAJOR_VERSION) > (maj) || \ + ((LIBLEPT_MAJOR_VERSION) == (maj) && (LIBLEPT_MINOR_VERSION) >= (min))) #else -# define TESSERACT_LIBLEPT_PREREQ(maj, min) 0 +#define TESSERACT_LIBLEPT_PREREQ(maj, min) 0 #endif -#if TESSERACT_LIBLEPT_PREREQ(1,73) -# define CALLOC LEPT_CALLOC -# define FREE LEPT_FREE +#if TESSERACT_LIBLEPT_PREREQ(1, 73) +#define CALLOC LEPT_CALLOC +#define FREE LEPT_FREE #endif #ifdef USE_OPENCL @@ -40,36 +41,28 @@ #include "opencl_device_selection.h" GPUEnv OpenclDevice::gpuEnv; - bool OpenclDevice::deviceIsSelected = false; ds_device OpenclDevice::selectedDevice; - int OpenclDevice::isInited = 0; static l_int32 MORPH_BC = ASYMMETRIC_MORPH_BC; static const l_uint32 lmask32[] = { - 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000, - 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000, - 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000, - 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000, - 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000, - 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00, - 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0, - 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff -}; + 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000, 0xf8000000, 0xfc000000, + 0xfe000000, 0xff000000, 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000, + 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000, 0xffff8000, 0xffffc000, + 0xffffe000, 0xfffff000, 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00, + 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0, 0xfffffff8, 0xfffffffc, + 0xfffffffe, 0xffffffff}; static const l_uint32 rmask32[] = { - 0x00000001, 0x00000003, 0x00000007, 0x0000000f, - 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, - 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff, - 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff, - 0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff, - 0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff, - 0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff, - 0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff -}; + 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 0x0000001f, 0x0000003f, + 0x0000007f, 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff, + 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff, 0x0001ffff, 0x0003ffff, + 0x0007ffff, 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff, + 0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff, 0x1fffffff, 0x3fffffff, + 0x7fffffff, 0xffffffff}; struct tiff_transform { int vflip; /* if non-zero, image needs a vertical fip */ @@ -90,7 +83,7 @@ static struct tiff_transform tiff_orientation_transforms[] = { {0, 0, -1} }; -static const l_int32 MAX_PAGES_IN_TIFF_FILE = 3000; +static const l_int32 MAX_PAGES_IN_TIFF_FILE = 3000; cl_mem pixsCLBuffer, pixdCLBuffer, pixdCLIntermediate; //Morph operations buffers cl_mem pixThBuffer; //output from thresholdtopix calculation @@ -100,7 +93,8 @@ KernelEnv rEnv; // substitute invalid characters in device name with _ void legalizeFileName( char *fileName) { //printf("fileName: %s\n", fileName); - const char* invalidChars = "/\?:*\"><| "; // space is valid but can cause headaches + const char *invalidChars = + "/\?:*\"><| "; // space is valid but can cause headaches // for each invalid char for (int i = 0; i < strlen(invalidChars); i++) { char invalidStr[4]; @@ -143,7 +137,6 @@ void populateGPUEnvFromDevice( GPUEnv *gpuInfo, cl_device_id device ) { cl_command_queue_properties queueProperties = 0; gpuInfo->mpCmdQueue = clCreateCommandQueue( gpuInfo->mpContext, gpuInfo->mpDevID, queueProperties, &clStatus ); CHECK_OPENCL( clStatus, "populateGPUEnv::createCommandQueue"); - } int OpenclDevice::LoadOpencl() @@ -159,7 +152,6 @@ int OpenclDevice::LoadOpencl() fprintf(stderr, "[OD] Load opencl.dll failed!\n"); FreeLibrary( static_cast( OpenclDll ) ); return 0; - } fprintf(stderr, "[OD] Load opencl.dll successful!\n"); #endif @@ -182,42 +174,37 @@ cl_mem allocateZeroCopyBuffer(KernelEnv rEnv, l_uint32 *hostbuffer, size_t nElem return membuffer; } -PIX* mapOutputCLBuffer(KernelEnv rEnv, cl_mem clbuffer, PIX* pixd, PIX* pixs, int elements, cl_mem_flags flags, bool memcopy = false, bool sync = true) -{ - PROCNAME("mapOutputCLBuffer"); - if (!pixd) - { - if (memcopy) - { - if ((pixd = pixCreateTemplate(pixs)) == NULL) - (PIX *)ERROR_PTR("pixd not made", procName, NULL); - } - else - { - if ((pixd = pixCreateHeader(pixGetWidth(pixs), pixGetHeight(pixs), pixGetDepth(pixs))) == NULL) - (PIX *)ERROR_PTR("pixd not made", procName, NULL); - } +PIX *mapOutputCLBuffer(KernelEnv rEnv, cl_mem clbuffer, PIX *pixd, PIX *pixs, + int elements, cl_mem_flags flags, bool memcopy = false, + bool sync = true) { + PROCNAME("mapOutputCLBuffer"); + if (!pixd) { + if (memcopy) { + if ((pixd = pixCreateTemplate(pixs)) == NULL) + (PIX *)ERROR_PTR("pixd not made", procName, NULL); + } else { + if ((pixd = pixCreateHeader(pixGetWidth(pixs), pixGetHeight(pixs), + pixGetDepth(pixs))) == NULL) + (PIX *)ERROR_PTR("pixd not made", procName, NULL); } - l_uint32 *pValues = (l_uint32 *)clEnqueueMapBuffer(rEnv.mpkCmdQueue, clbuffer, CL_TRUE, flags, 0, - elements * sizeof(l_uint32), 0, NULL, NULL, NULL ); + } + l_uint32 *pValues = (l_uint32 *)clEnqueueMapBuffer( + rEnv.mpkCmdQueue, clbuffer, CL_TRUE, flags, 0, + elements * sizeof(l_uint32), 0, NULL, NULL, NULL); - if (memcopy) - { - memcpy(pixGetData(pixd), pValues, elements * sizeof(l_uint32)); - } - else - { - pixSetData(pixd, pValues); - } + if (memcopy) { + memcpy(pixGetData(pixd), pValues, elements * sizeof(l_uint32)); + } else { + pixSetData(pixd, pValues); + } - clEnqueueUnmapMemObject(rEnv.mpkCmdQueue,clbuffer,pValues,0,NULL,NULL); + clEnqueueUnmapMemObject(rEnv.mpkCmdQueue, clbuffer, pValues, 0, NULL, NULL); - if (sync) - { - clFinish( rEnv.mpkCmdQueue ); - } + if (sync) { + clFinish(rEnv.mpkCmdQueue); + } - return pixd; + return pixd; } cl_mem allocateIntBuffer( KernelEnv rEnv, const l_uint32 *_pValues, size_t nElements, cl_int *pStatus , bool sync = false) @@ -252,7 +239,7 @@ void OpenclDevice::releaseMorphCLBuffers() clReleaseMemObject(pixdCLBuffer); if (pixThBuffer != NULL) clReleaseMemObject(pixThBuffer); - pixdCLIntermediate = pixsCLBuffer = pixdCLBuffer = pixThBuffer = NULL; + pixdCLIntermediate = pixsCLBuffer = pixdCLBuffer = pixThBuffer = NULL; } int OpenclDevice::initMorphCLAllocations(l_int32 wpl, l_int32 h, PIX* pixs) @@ -296,7 +283,6 @@ PERF_COUNT_SUB("LoadOpencl") #endif // sets up environment, compiles programs - InitOpenclRunEnv_DeviceSelection( 0 ); //PERF_COUNT_SUB("called InitOpenclRunEnv_DS") //PERF_COUNT_END @@ -409,17 +395,18 @@ int OpenclDevice::BinaryGenerated( const char * clFileName, FILE ** fhandle ) int status = 0; char *str = NULL; FILE *fd = NULL; - char fileName[256] = { 0 }, cl_name[128] = { 0 }; + char fileName[256] = {0}, cl_name[128] = {0}; char deviceName[1024]; - clStatus = clGetDeviceInfo( gpuEnv.mpArryDevsID[i], CL_DEVICE_NAME, sizeof(deviceName), deviceName, NULL ); - CHECK_OPENCL( clStatus, "clGetDeviceInfo" ); - str = (char*) strstr( clFileName, (char*) ".cl" ); - memcpy( cl_name, clFileName, str - clFileName ); + clStatus = clGetDeviceInfo(gpuEnv.mpArryDevsID[i], CL_DEVICE_NAME, + sizeof(deviceName), deviceName, NULL); + CHECK_OPENCL(clStatus, "clGetDeviceInfo"); + str = (char *)strstr(clFileName, (char *)".cl"); + memcpy(cl_name, clFileName, str - clFileName); cl_name[str - clFileName] = '\0'; - sprintf( fileName, "%s-%s.bin", cl_name, deviceName ); + sprintf(fileName, "%s-%s.bin", cl_name, deviceName); legalizeFileName(fileName); - fd = fopen( fileName, "rb" ); - status = ( fd != NULL ) ? 1 : 0; + fd = fopen(fileName, "rb"); + status = (fd != NULL) ? 1 : 0; if ( fd != NULL ) { *fhandle = fd; @@ -462,7 +449,7 @@ int OpenclDevice::GeneratBinFromKernelSource( cl_program program, const char * c { unsigned int i = 0; cl_int clStatus; - size_t *binarySizes, numDevices=0; + size_t *binarySizes, numDevices = 0; cl_device_id *mpArryDevsID; char **binaries, *str = NULL; @@ -838,7 +825,6 @@ PIX *pix; fclose(fp); PERF_COUNT_END return pix; - } TIFF * OpenclDevice::fopenTiffCl(FILE *fp, @@ -1039,8 +1025,8 @@ tiffCloseCallback(thandle_t handle) *mstream->poutdata = mstream->buffer; *mstream->poutsize = mstream->hw; } - FREE(mstream); /* never free the buffer! */ - return 0; + FREE(mstream); /* never free the buffer! */ + return 0; } @@ -1134,38 +1120,38 @@ OpenclDevice::pixReadMemTiffCl(const l_uint8 *data,size_t size,l_int32 n) l_int32 i, pagefound; PIX *pix; TIFF *tif; - //L_MEMSTREAM *memStream; - PROCNAME("pixReadMemTiffCl"); + // L_MEMSTREAM *memStream; + PROCNAME("pixReadMemTiffCl"); - if (!data) - return (PIX *)ERROR_PTR("data pointer is NULL", procName, NULL); + if (!data) + return (PIX *)ERROR_PTR("data pointer is NULL", procName, NULL); - if ((tif = fopenTiffMemstream("", "r", (l_uint8 **)&data, &size)) == NULL) - return (PIX *)ERROR_PTR("tif not opened", procName, NULL); + if ((tif = fopenTiffMemstream("", "r", (l_uint8 **)&data, &size)) == + NULL) + return (PIX *)ERROR_PTR("tif not opened", procName, NULL); - pagefound = FALSE; - pix = NULL; - for (i = 0; i < MAX_PAGES_IN_TIFF_FILE; i++) { - if (i == n) { - pagefound = TRUE; - if ((pix = pixReadFromTiffStreamCl(tif)) == NULL) { - TIFFCleanup(tif); - return (PIX *)ERROR_PTR("pix not read", procName, NULL); - } - break; - } - if (TIFFReadDirectory(tif) == 0) - break; - } + pagefound = FALSE; + pix = NULL; + for (i = 0; i < MAX_PAGES_IN_TIFF_FILE; i++) { + if (i == n) { + pagefound = TRUE; + if ((pix = pixReadFromTiffStreamCl(tif)) == NULL) { + TIFFCleanup(tif); + return (PIX *)ERROR_PTR("pix not read", procName, NULL); + } + break; + } + if (TIFFReadDirectory(tif) == 0) break; + } - if (pagefound == FALSE) { - L_WARNING("tiff page %d not found", procName); - TIFFCleanup(tif); - return NULL; - } + if (pagefound == FALSE) { + L_WARNING("tiff page %d not found", procName); + TIFFCleanup(tif); + return NULL; + } - TIFFCleanup(tif); - return pix; + TIFFCleanup(tif); + return pix; } PIX * @@ -1252,7 +1238,6 @@ void compare(l_uint32 *cpu, l_uint32 *gpu,int size) } } printf("\nit matches\n"); - } //OpenCL implementation of pixReadFromTiffStream. @@ -1275,7 +1260,6 @@ PIXCMAP *cmap; if (!tif) return (PIX *)ERROR_PTR("tif not defined", procName, NULL); - TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &bps); TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &spp); bpp = bps * spp; @@ -1298,16 +1282,15 @@ PIXCMAP *cmap; wpl = pixGetWpl(pix); bpl = 4 * wpl; - if (spp == 1) { if ((linebuf = (l_uint8 *)CALLOC(tiffbpl + 1, sizeof(l_uint8))) == NULL) return (PIX *)ERROR_PTR("calloc fail for linebuf", procName, NULL); for (i = 0 ; i < h ; i++) { if (TIFFReadScanline(tif, linebuf, i, 0) < 0) { - FREE(linebuf); - pixDestroy(&pix); - return (PIX *)ERROR_PTR("line read fail", procName, NULL); + FREE(linebuf); + pixDestroy(&pix); + return (PIX *)ERROR_PTR("line read fail", procName, NULL); } memcpy((char *)data, (char *)linebuf, tiffbpl); data += bpl; @@ -1315,30 +1298,29 @@ PIXCMAP *cmap; if (bps <= 8) pixEndianByteSwap(pix); else - pixEndianTwoByteSwap(pix); + pixEndianTwoByteSwap(pix); FREE(linebuf); - } - else { - if ((tiffdata = (l_uint32 *)CALLOC(w * h, sizeof(l_uint32))) == NULL) { - pixDestroy(&pix); - return (PIX *)ERROR_PTR("calloc fail for tiffdata", procName, NULL); - } - if (!TIFFReadRGBAImageOriented(tif, w, h, (uint32 *)tiffdata, - ORIENTATION_TOPLEFT, 0)) { - FREE(tiffdata); - pixDestroy(&pix); - return (PIX *)ERROR_PTR("failed to read tiffdata", procName, NULL); - } - line = pixGetData(pix); - - //Invoke the OpenCL kernel for pixReadFromTiff - l_uint32* output_gpu=pixReadFromTiffKernel(tiffdata,w,h,wpl,line); - - pixSetData(pix, output_gpu); - // pix already has data allocated, it now points to output_gpu? + } else { + if ((tiffdata = (l_uint32 *)CALLOC(w * h, sizeof(l_uint32))) == NULL) { + pixDestroy(&pix); + return (PIX *)ERROR_PTR("calloc fail for tiffdata", procName, NULL); + } + if (!TIFFReadRGBAImageOriented(tif, w, h, (uint32 *)tiffdata, + ORIENTATION_TOPLEFT, 0)) { FREE(tiffdata); - FREE(line); - //FREE(output_gpu); + pixDestroy(&pix); + return (PIX *)ERROR_PTR("failed to read tiffdata", procName, NULL); + } + line = pixGetData(pix); + + // Invoke the OpenCL kernel for pixReadFromTiff + l_uint32 *output_gpu = pixReadFromTiffKernel(tiffdata, w, h, wpl, line); + + pixSetData(pix, output_gpu); + // pix already has data allocated, it now points to output_gpu? + FREE(tiffdata); + FREE(line); + // FREE(output_gpu); } if (getTiffStreamResolutionCl(tif, &xres, &yres) == 0) { @@ -1352,7 +1334,6 @@ PIXCMAP *cmap; pixSetInputFormat(pix, comptype); if (TIFFGetField(tif, TIFFTAG_COLORMAP, &redmap, &greenmap, &bluemap)) { - if ((cmap = pixcmapCreate(bps)) == NULL) { pixDestroy(&pix); return (PIX *)ERROR_PTR("cmap not made", procName, NULL); @@ -1362,22 +1343,19 @@ PIXCMAP *cmap; pixcmapAddColor(cmap, redmap[i] >> 8, greenmap[i] >> 8, bluemap[i] >> 8); pixSetColormap(pix, cmap); - } - else { - if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometry)) { - - if (tiffcomp == COMPRESSION_CCITTFAX3 || - tiffcomp == COMPRESSION_CCITTFAX4 || - tiffcomp == COMPRESSION_CCITTRLE || - tiffcomp == COMPRESSION_CCITTRLEW) { - photometry = PHOTOMETRIC_MINISWHITE; - } - else - photometry = PHOTOMETRIC_MINISBLACK; - } - if ((d == 1 && photometry == PHOTOMETRIC_MINISBLACK) || - (d == 8 && photometry == PHOTOMETRIC_MINISWHITE)) - pixInvert(pix, pix); + } else { + if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometry)) { + if (tiffcomp == COMPRESSION_CCITTFAX3 || + tiffcomp == COMPRESSION_CCITTFAX4 || + tiffcomp == COMPRESSION_CCITTRLE || + tiffcomp == COMPRESSION_CCITTRLEW) { + photometry = PHOTOMETRIC_MINISWHITE; + } else + photometry = PHOTOMETRIC_MINISBLACK; + } + if ((d == 1 && photometry == PHOTOMETRIC_MINISBLACK) || + (d == 8 && photometry == PHOTOMETRIC_MINISWHITE)) + pixInvert(pix, pix); } if (TIFFGetField(tif, TIFFTAG_ORIENTATION, &orientation)) { @@ -1625,95 +1603,50 @@ pixDilateCL(l_int32 hsize, l_int32 vsize, l_int32 wpl, l_int32 h) if (xp > 31 || xn > 31) { - //Generic case. - rEnv.mpkKernel = clCreateKernel( rEnv.mpkProgram, "morphoDilateHor", &status ); + // Generic case. + rEnv.mpkKernel = + clCreateKernel(rEnv.mpkProgram, "morphoDilateHor", &status); - status = clSetKernelArg(rEnv.mpkKernel, - 0, - sizeof(cl_mem), - &pixsCLBuffer); - status = clSetKernelArg(rEnv.mpkKernel, - 1, - sizeof(cl_mem), - &pixdCLBuffer); - status = clSetKernelArg(rEnv.mpkKernel, - 2, - sizeof(xp), - (const void *)&xp); - status = clSetKernelArg(rEnv.mpkKernel, - 3, - sizeof(xn), - (const void *)&xn); - status = clSetKernelArg(rEnv.mpkKernel, - 4, - sizeof(wpl), - (const void *)&wpl); - status = clSetKernelArg(rEnv.mpkKernel, - 5, - sizeof(h), - (const void *)&h); - status = clEnqueueNDRangeKernel(rEnv.mpkCmdQueue, - rEnv.mpkKernel, - 2, - NULL, - globalThreads, - localThreads, - 0, - NULL, - NULL); + status = clSetKernelArg(rEnv.mpkKernel, 0, sizeof(cl_mem), &pixsCLBuffer); + status = clSetKernelArg(rEnv.mpkKernel, 1, sizeof(cl_mem), &pixdCLBuffer); + status = clSetKernelArg(rEnv.mpkKernel, 2, sizeof(xp), (const void *)&xp); + status = clSetKernelArg(rEnv.mpkKernel, 3, sizeof(xn), (const void *)&xn); + status = + clSetKernelArg(rEnv.mpkKernel, 4, sizeof(wpl), (const void *)&wpl); + status = clSetKernelArg(rEnv.mpkKernel, 5, sizeof(h), (const void *)&h); + status = + clEnqueueNDRangeKernel(rEnv.mpkCmdQueue, rEnv.mpkKernel, 2, NULL, + globalThreads, localThreads, 0, NULL, NULL); - if (yp > 0 || yn > 0) - { - pixtemp = pixsCLBuffer; - pixsCLBuffer = pixdCLBuffer; - pixdCLBuffer = pixtemp; + if (yp > 0 || yn > 0) { + pixtemp = pixsCLBuffer; + pixsCLBuffer = pixdCLBuffer; + pixdCLBuffer = pixtemp; } } else if (xp > 0 || xn > 0 ) { - //Specific Horizontal pass kernel for half width < 32 - rEnv.mpkKernel = clCreateKernel( rEnv.mpkProgram, "morphoDilateHor_32word", &status ); - isEven = (xp != xn); + // Specific Horizontal pass kernel for half width < 32 + rEnv.mpkKernel = + clCreateKernel(rEnv.mpkProgram, "morphoDilateHor_32word", &status); + isEven = (xp != xn); - status = clSetKernelArg(rEnv.mpkKernel, - 0, - sizeof(cl_mem), - &pixsCLBuffer); - status = clSetKernelArg(rEnv.mpkKernel, - 1, - sizeof(cl_mem), - &pixdCLBuffer); - status = clSetKernelArg(rEnv.mpkKernel, - 2, - sizeof(xp), - (const void *)&xp); - status = clSetKernelArg(rEnv.mpkKernel, - 3, - sizeof(wpl), - (const void *)&wpl); - status = clSetKernelArg(rEnv.mpkKernel, - 4, - sizeof(h), - (const void *)&h); - status = clSetKernelArg(rEnv.mpkKernel, - 5, - sizeof(isEven), - (const void *)&isEven); - status = clEnqueueNDRangeKernel(rEnv.mpkCmdQueue, - rEnv.mpkKernel, - 2, - NULL, - globalThreads, - localThreads, - 0, - NULL, - NULL); + status = clSetKernelArg(rEnv.mpkKernel, 0, sizeof(cl_mem), &pixsCLBuffer); + status = clSetKernelArg(rEnv.mpkKernel, 1, sizeof(cl_mem), &pixdCLBuffer); + status = clSetKernelArg(rEnv.mpkKernel, 2, sizeof(xp), (const void *)&xp); + status = + clSetKernelArg(rEnv.mpkKernel, 3, sizeof(wpl), (const void *)&wpl); + status = clSetKernelArg(rEnv.mpkKernel, 4, sizeof(h), (const void *)&h); + status = clSetKernelArg(rEnv.mpkKernel, 5, sizeof(isEven), + (const void *)&isEven); + status = + clEnqueueNDRangeKernel(rEnv.mpkCmdQueue, rEnv.mpkKernel, 2, NULL, + globalThreads, localThreads, 0, NULL, NULL); - if (yp > 0 || yn > 0) - { - pixtemp = pixsCLBuffer; - pixsCLBuffer = pixdCLBuffer; - pixdCLBuffer = pixtemp; + if (yp > 0 || yn > 0) { + pixtemp = pixsCLBuffer; + pixsCLBuffer = pixdCLBuffer; + pixdCLBuffer = pixtemp; } } @@ -1756,253 +1689,153 @@ pixDilateCL(l_int32 hsize, l_int32 vsize, l_int32 wpl, l_int32 h) NULL); } - return status; } //Morphology Erode operation. Invokes the relevant OpenCL kernels -cl_int -pixErodeCL(l_int32 hsize, l_int32 vsize, l_uint32 wpl, l_uint32 h) -{ +cl_int pixErodeCL(l_int32 hsize, l_int32 vsize, l_uint32 wpl, l_uint32 h) { + l_int32 xp, yp, xn, yn; + SEL *sel; + size_t globalThreads[2]; + size_t localThreads[2]; + cl_mem pixtemp; + cl_int status; + int gsize; + char isAsymmetric = (MORPH_BC == ASYMMETRIC_MORPH_BC); + l_uint32 rwmask, lwmask; + char isEven; - l_int32 xp, yp, xn, yn; - SEL* sel; - size_t globalThreads[2]; - size_t localThreads[2]; - cl_mem pixtemp; - cl_int status; - int gsize; - char isAsymmetric = (MORPH_BC == ASYMMETRIC_MORPH_BC); - l_uint32 rwmask, lwmask; - char isEven; + sel = selCreateBrick(vsize, hsize, vsize / 2, hsize / 2, SEL_HIT); - sel = selCreateBrick(vsize, hsize, vsize / 2, hsize / 2, SEL_HIT); - - selFindMaxTranslations(sel, &xp, &yp, &xn, &yn); - selDestroy(&sel); - OpenclDevice::SetKernelEnv( &rEnv ); - - if (hsize == 5 && vsize == 5 && isAsymmetric) - { - //Specific kernel for 5x5 - status = pixErodeCL_55(wpl, h); - return status; - } - - lwmask = lmask32[31 - (xn & 31)]; - rwmask = rmask32[31 - (xp & 31)]; - - //global and local work dimensions for Horizontal pass - gsize = (wpl + GROUPSIZE_X - 1)/ GROUPSIZE_X * GROUPSIZE_X; - globalThreads[0] = gsize; - gsize = (h + GROUPSIZE_Y - 1)/ GROUPSIZE_Y * GROUPSIZE_Y; - globalThreads[1] = gsize; - localThreads[0] = GROUPSIZE_X; - localThreads[1] = GROUPSIZE_Y; - - //Horizontal Pass - if (xp > 31 || xn > 31 ) - { - //Generic case. - rEnv.mpkKernel = clCreateKernel( rEnv.mpkProgram, "morphoErodeHor", &status ); - - status = clSetKernelArg(rEnv.mpkKernel, - 0, - sizeof(cl_mem), - &pixsCLBuffer); - status = clSetKernelArg(rEnv.mpkKernel, - 1, - sizeof(cl_mem), - &pixdCLBuffer); - status = clSetKernelArg(rEnv.mpkKernel, - 2, - sizeof(xp), - (const void *)&xp); - status = clSetKernelArg(rEnv.mpkKernel, - 3, - sizeof(xn), - (const void *)&xn); - status = clSetKernelArg(rEnv.mpkKernel, - 4, - sizeof(wpl), - (const void *)&wpl); - status = clSetKernelArg(rEnv.mpkKernel, - 5, - sizeof(h), - (const void *)&h); - status = clSetKernelArg(rEnv.mpkKernel, - 6, - sizeof(isAsymmetric), - (const void *)&isAsymmetric); - status = clSetKernelArg(rEnv.mpkKernel, - 7, - sizeof(rwmask), - (const void *)&rwmask); - status = clSetKernelArg(rEnv.mpkKernel, - 8, - sizeof(lwmask), - (const void *)&lwmask); - status = clEnqueueNDRangeKernel(rEnv.mpkCmdQueue, - rEnv.mpkKernel, - 2, - NULL, - globalThreads, - localThreads, - 0, - NULL, - NULL); - - if (yp > 0 || yn > 0) - { - pixtemp = pixsCLBuffer; - pixsCLBuffer = pixdCLBuffer; - pixdCLBuffer = pixtemp; - } - } - else if (xp > 0 || xn > 0) - { - rEnv.mpkKernel = clCreateKernel( rEnv.mpkProgram, "morphoErodeHor_32word", &status ); - isEven = (xp != xn); - - status = clSetKernelArg(rEnv.mpkKernel, - 0, - sizeof(cl_mem), - &pixsCLBuffer); - status = clSetKernelArg(rEnv.mpkKernel, - 1, - sizeof(cl_mem), - &pixdCLBuffer); - status = clSetKernelArg(rEnv.mpkKernel, - 2, - sizeof(xp), - (const void *)&xp); - status = clSetKernelArg(rEnv.mpkKernel, - 3, - sizeof(wpl), - (const void *)&wpl); - status = clSetKernelArg(rEnv.mpkKernel, - 4, - sizeof(h), - (const void *)&h); - status = clSetKernelArg(rEnv.mpkKernel, - 5, - sizeof(isAsymmetric), - (const void *)&isAsymmetric); - status = clSetKernelArg(rEnv.mpkKernel, - 6, - sizeof(rwmask), - (const void *)&rwmask); - status = clSetKernelArg(rEnv.mpkKernel, - 7, - sizeof(lwmask), - (const void *)&lwmask); - status = clSetKernelArg(rEnv.mpkKernel, - 8, - sizeof(isEven), - (const void *)&isEven); - status = clEnqueueNDRangeKernel(rEnv.mpkCmdQueue, - rEnv.mpkKernel, - 2, - NULL, - globalThreads, - localThreads, - 0, - NULL, - NULL); - - if (yp > 0 || yn > 0) - { - pixtemp = pixsCLBuffer; - pixsCLBuffer = pixdCLBuffer; - pixdCLBuffer = pixtemp; - } - } - - //Vertical Pass - if (yp > 0 || yn > 0) - { - rEnv.mpkKernel = clCreateKernel( rEnv.mpkProgram, "morphoErodeVer", &status ); - - status = clSetKernelArg(rEnv.mpkKernel, - 0, - sizeof(cl_mem), - &pixsCLBuffer); - status = clSetKernelArg(rEnv.mpkKernel, - 1, - sizeof(cl_mem), - &pixdCLBuffer); - status = clSetKernelArg(rEnv.mpkKernel, - 2, - sizeof(yp), - (const void *)&yp); - status = clSetKernelArg(rEnv.mpkKernel, - 3, - sizeof(wpl), - (const void *)&wpl); - status = clSetKernelArg(rEnv.mpkKernel, - 4, - sizeof(h), - (const void *)&h); - status = clSetKernelArg(rEnv.mpkKernel, - 5, - sizeof(isAsymmetric), - (const void *)&isAsymmetric); - status = clSetKernelArg(rEnv.mpkKernel, - 6, - sizeof(yn), - (const void *)&yn); - status = clEnqueueNDRangeKernel(rEnv.mpkCmdQueue, - rEnv.mpkKernel, - 2, - NULL, - globalThreads, - localThreads, - 0, - NULL, - NULL); - } + selFindMaxTranslations(sel, &xp, &yp, &xn, &yn); + selDestroy(&sel); + OpenclDevice::SetKernelEnv(&rEnv); + if (hsize == 5 && vsize == 5 && isAsymmetric) { + // Specific kernel for 5x5 + status = pixErodeCL_55(wpl, h); return status; + } + + lwmask = lmask32[31 - (xn & 31)]; + rwmask = rmask32[31 - (xp & 31)]; + + // global and local work dimensions for Horizontal pass + gsize = (wpl + GROUPSIZE_X - 1) / GROUPSIZE_X * GROUPSIZE_X; + globalThreads[0] = gsize; + gsize = (h + GROUPSIZE_Y - 1) / GROUPSIZE_Y * GROUPSIZE_Y; + globalThreads[1] = gsize; + localThreads[0] = GROUPSIZE_X; + localThreads[1] = GROUPSIZE_Y; + + // Horizontal Pass + if (xp > 31 || xn > 31) { + // Generic case. + rEnv.mpkKernel = clCreateKernel(rEnv.mpkProgram, "morphoErodeHor", &status); + + status = clSetKernelArg(rEnv.mpkKernel, 0, sizeof(cl_mem), &pixsCLBuffer); + status = clSetKernelArg(rEnv.mpkKernel, 1, sizeof(cl_mem), &pixdCLBuffer); + status = clSetKernelArg(rEnv.mpkKernel, 2, sizeof(xp), (const void *)&xp); + status = clSetKernelArg(rEnv.mpkKernel, 3, sizeof(xn), (const void *)&xn); + status = clSetKernelArg(rEnv.mpkKernel, 4, sizeof(wpl), (const void *)&wpl); + status = clSetKernelArg(rEnv.mpkKernel, 5, sizeof(h), (const void *)&h); + status = clSetKernelArg(rEnv.mpkKernel, 6, sizeof(isAsymmetric), + (const void *)&isAsymmetric); + status = clSetKernelArg(rEnv.mpkKernel, 7, sizeof(rwmask), + (const void *)&rwmask); + status = clSetKernelArg(rEnv.mpkKernel, 8, sizeof(lwmask), + (const void *)&lwmask); + status = clEnqueueNDRangeKernel(rEnv.mpkCmdQueue, rEnv.mpkKernel, 2, NULL, + globalThreads, localThreads, 0, NULL, NULL); + + if (yp > 0 || yn > 0) { + pixtemp = pixsCLBuffer; + pixsCLBuffer = pixdCLBuffer; + pixdCLBuffer = pixtemp; + } + } else if (xp > 0 || xn > 0) { + rEnv.mpkKernel = + clCreateKernel(rEnv.mpkProgram, "morphoErodeHor_32word", &status); + isEven = (xp != xn); + + status = clSetKernelArg(rEnv.mpkKernel, 0, sizeof(cl_mem), &pixsCLBuffer); + status = clSetKernelArg(rEnv.mpkKernel, 1, sizeof(cl_mem), &pixdCLBuffer); + status = clSetKernelArg(rEnv.mpkKernel, 2, sizeof(xp), (const void *)&xp); + status = clSetKernelArg(rEnv.mpkKernel, 3, sizeof(wpl), (const void *)&wpl); + status = clSetKernelArg(rEnv.mpkKernel, 4, sizeof(h), (const void *)&h); + status = clSetKernelArg(rEnv.mpkKernel, 5, sizeof(isAsymmetric), + (const void *)&isAsymmetric); + status = clSetKernelArg(rEnv.mpkKernel, 6, sizeof(rwmask), + (const void *)&rwmask); + status = clSetKernelArg(rEnv.mpkKernel, 7, sizeof(lwmask), + (const void *)&lwmask); + status = clSetKernelArg(rEnv.mpkKernel, 8, sizeof(isEven), + (const void *)&isEven); + status = clEnqueueNDRangeKernel(rEnv.mpkCmdQueue, rEnv.mpkKernel, 2, NULL, + globalThreads, localThreads, 0, NULL, NULL); + + if (yp > 0 || yn > 0) { + pixtemp = pixsCLBuffer; + pixsCLBuffer = pixdCLBuffer; + pixdCLBuffer = pixtemp; + } + } + + // Vertical Pass + if (yp > 0 || yn > 0) { + rEnv.mpkKernel = clCreateKernel(rEnv.mpkProgram, "morphoErodeVer", &status); + + status = clSetKernelArg(rEnv.mpkKernel, 0, sizeof(cl_mem), &pixsCLBuffer); + status = clSetKernelArg(rEnv.mpkKernel, 1, sizeof(cl_mem), &pixdCLBuffer); + status = clSetKernelArg(rEnv.mpkKernel, 2, sizeof(yp), (const void *)&yp); + status = clSetKernelArg(rEnv.mpkKernel, 3, sizeof(wpl), (const void *)&wpl); + status = clSetKernelArg(rEnv.mpkKernel, 4, sizeof(h), (const void *)&h); + status = clSetKernelArg(rEnv.mpkKernel, 5, sizeof(isAsymmetric), + (const void *)&isAsymmetric); + status = clSetKernelArg(rEnv.mpkKernel, 6, sizeof(yn), (const void *)&yn); + status = clEnqueueNDRangeKernel(rEnv.mpkCmdQueue, rEnv.mpkKernel, 2, NULL, + globalThreads, localThreads, 0, NULL, NULL); + } + + return status; } // OpenCL implementation of Morphology Dilate //Note: Assumes the source and dest opencl buffer are initialized. No check done -PIX* -OpenclDevice::pixDilateBrickCL(PIX *pixd, PIX *pixs, l_int32 hsize, l_int32 vsize, bool reqDataCopy = false) -{ - l_uint32 wpl, h; +PIX *OpenclDevice::pixDilateBrickCL(PIX *pixd, PIX *pixs, l_int32 hsize, + l_int32 vsize, bool reqDataCopy = false) { + l_uint32 wpl, h; - wpl = pixGetWpl(pixs); - h = pixGetHeight(pixs); + wpl = pixGetWpl(pixs); + h = pixGetHeight(pixs); - clStatus = pixDilateCL(hsize, vsize, wpl, h); + clStatus = pixDilateCL(hsize, vsize, wpl, h); - if (reqDataCopy) - { - pixd = mapOutputCLBuffer(rEnv, pixdCLBuffer, pixd, pixs, wpl*h, CL_MAP_READ, false); - } + if (reqDataCopy) { + pixd = mapOutputCLBuffer(rEnv, pixdCLBuffer, pixd, pixs, wpl * h, + CL_MAP_READ, false); + } - return pixd; + return pixd; } // OpenCL implementation of Morphology Erode //Note: Assumes the source and dest opencl buffer are initialized. No check done -PIX* -OpenclDevice::pixErodeBrickCL(PIX *pixd, PIX *pixs, l_int32 hsize, l_int32 vsize, bool reqDataCopy = false) -{ - l_uint32 wpl, h; +PIX *OpenclDevice::pixErodeBrickCL(PIX *pixd, PIX *pixs, l_int32 hsize, + l_int32 vsize, bool reqDataCopy = false) { + l_uint32 wpl, h; - wpl = pixGetWpl(pixs); - h = pixGetHeight(pixs); + wpl = pixGetWpl(pixs); + h = pixGetHeight(pixs); - clStatus = pixErodeCL(hsize, vsize, wpl, h); + clStatus = pixErodeCL(hsize, vsize, wpl, h); - if (reqDataCopy) - { - pixd = mapOutputCLBuffer(rEnv, pixdCLBuffer, pixd, pixs, wpl*h, CL_MAP_READ); - } + if (reqDataCopy) { + pixd = + mapOutputCLBuffer(rEnv, pixdCLBuffer, pixd, pixs, wpl * h, CL_MAP_READ); + } - return pixd; + return pixd; } //Morphology Open operation. Invokes the relevant OpenCL kernels @@ -2045,50 +1878,40 @@ pixCloseCL(l_int32 hsize, l_int32 vsize, l_int32 wpl, l_int32 h) // OpenCL implementation of Morphology Close //Note: Assumes the source and dest opencl buffer are initialized. No check done -PIX* -OpenclDevice::pixCloseBrickCL(PIX *pixd, - PIX *pixs, - l_int32 hsize, - l_int32 vsize, - bool reqDataCopy = false) -{ - l_uint32 wpl, h; +PIX *OpenclDevice::pixCloseBrickCL(PIX *pixd, PIX *pixs, l_int32 hsize, + l_int32 vsize, bool reqDataCopy = false) { + l_uint32 wpl, h; - wpl = pixGetWpl(pixs); - h = pixGetHeight(pixs); + wpl = pixGetWpl(pixs); + h = pixGetHeight(pixs); - clStatus = pixCloseCL(hsize, vsize, wpl, h); + clStatus = pixCloseCL(hsize, vsize, wpl, h); - if (reqDataCopy) - { - pixd = mapOutputCLBuffer(rEnv, pixdCLBuffer, pixd, pixs, wpl*h, CL_MAP_READ); - } + if (reqDataCopy) { + pixd = + mapOutputCLBuffer(rEnv, pixdCLBuffer, pixd, pixs, wpl * h, CL_MAP_READ); + } - return pixd; + return pixd; } // OpenCL implementation of Morphology Open //Note: Assumes the source and dest opencl buffer are initialized. No check done -PIX* -OpenclDevice::pixOpenBrickCL(PIX *pixd, - PIX *pixs, - l_int32 hsize, - l_int32 vsize, - bool reqDataCopy = false) -{ - l_uint32 wpl, h; +PIX *OpenclDevice::pixOpenBrickCL(PIX *pixd, PIX *pixs, l_int32 hsize, + l_int32 vsize, bool reqDataCopy = false) { + l_uint32 wpl, h; - wpl = pixGetWpl(pixs); - h = pixGetHeight(pixs); + wpl = pixGetWpl(pixs); + h = pixGetHeight(pixs); - clStatus = pixOpenCL(hsize, vsize, wpl, h); + clStatus = pixOpenCL(hsize, vsize, wpl, h); - if (reqDataCopy) - { - pixd = mapOutputCLBuffer(rEnv, pixdCLBuffer, pixd, pixs, wpl*h, CL_MAP_READ); - } + if (reqDataCopy) { + pixd = + mapOutputCLBuffer(rEnv, pixdCLBuffer, pixd, pixs, wpl * h, CL_MAP_READ); + } - return pixd; + return pixd; } //pix OR operation: outbuffer = buffer1 | buffer2 @@ -2252,19 +2075,16 @@ pixSubtractCL_work(l_uint32 wpl, l_uint32 h, cl_mem buffer1, cl_mem buffer2, cl_ // OpenCL implementation of Subtract pix //Note: Assumes the source and dest opencl buffer are initialized. No check done -PIX* -OpenclDevice::pixSubtractCL(PIX *pixd, PIX *pixs1, PIX *pixs2, bool reqDataCopy = false) -{ - l_uint32 wpl, h; +PIX *OpenclDevice::pixSubtractCL(PIX *pixd, PIX *pixs1, PIX *pixs2, + bool reqDataCopy = false) { + l_uint32 wpl, h; - PROCNAME("pixSubtractCL"); + PROCNAME("pixSubtractCL"); - if (!pixs1) - return (PIX *)ERROR_PTR("pixs1 not defined", procName, pixd); - if (!pixs2) - return (PIX *)ERROR_PTR("pixs2 not defined", procName, pixd); - if (pixGetDepth(pixs1) != pixGetDepth(pixs2)) - return (PIX *)ERROR_PTR("depths of pixs* unequal", procName, pixd); + if (!pixs1) return (PIX *)ERROR_PTR("pixs1 not defined", procName, pixd); + if (!pixs2) return (PIX *)ERROR_PTR("pixs2 not defined", procName, pixd); + if (pixGetDepth(pixs1) != pixGetDepth(pixs2)) + return (PIX *)ERROR_PTR("depths of pixs* unequal", procName, pixd); #if EQUAL_SIZE_WARNING if (!pixSizesEqual(pixs1, pixs2)) @@ -2287,200 +2107,207 @@ OpenclDevice::pixSubtractCL(PIX *pixd, PIX *pixs1, PIX *pixs2, bool reqDataCo // OpenCL implementation of Hollow pix //Note: Assumes the source and dest opencl buffer are initialized. No check done -PIX* -OpenclDevice::pixHollowCL(PIX *pixd, - PIX *pixs, - l_int32 close_hsize, - l_int32 close_vsize, - l_int32 open_hsize, - l_int32 open_vsize, - bool reqDataCopy = false) -{ - l_uint32 wpl, h; - cl_mem pixtemp; +PIX *OpenclDevice::pixHollowCL(PIX *pixd, PIX *pixs, l_int32 close_hsize, + l_int32 close_vsize, l_int32 open_hsize, + l_int32 open_vsize, bool reqDataCopy = false) { + l_uint32 wpl, h; + cl_mem pixtemp; - wpl = pixGetWpl(pixs); - h = pixGetHeight(pixs); + wpl = pixGetWpl(pixs); + h = pixGetHeight(pixs); - //First step : Close Morph operation: Dilate followed by Erode - clStatus = pixCloseCL(close_hsize, close_vsize, wpl, h); + // First step : Close Morph operation: Dilate followed by Erode + clStatus = pixCloseCL(close_hsize, close_vsize, wpl, h); - //Store the output of close operation in an intermediate buffer - //this will be later used for pixsubtract - clStatus = clEnqueueCopyBuffer(rEnv.mpkCmdQueue, pixdCLBuffer, pixdCLIntermediate, 0, 0, sizeof(int) * wpl*h, 0, NULL, NULL); + // Store the output of close operation in an intermediate buffer + // this will be later used for pixsubtract + clStatus = + clEnqueueCopyBuffer(rEnv.mpkCmdQueue, pixdCLBuffer, pixdCLIntermediate, 0, + 0, sizeof(int) * wpl * h, 0, NULL, NULL); - //Second step: Open Operation - Erode followed by Dilate - pixtemp = pixsCLBuffer; - pixsCLBuffer = pixdCLBuffer; - pixdCLBuffer = pixtemp; + // Second step: Open Operation - Erode followed by Dilate + pixtemp = pixsCLBuffer; + pixsCLBuffer = pixdCLBuffer; + pixdCLBuffer = pixtemp; - clStatus = pixOpenCL(open_hsize, open_vsize, wpl, h); + clStatus = pixOpenCL(open_hsize, open_vsize, wpl, h); - //Third step: Subtract : (Close - Open) - pixtemp = pixsCLBuffer; - pixsCLBuffer = pixdCLBuffer; - pixdCLBuffer = pixdCLIntermediate; - pixdCLIntermediate = pixtemp; + // Third step: Subtract : (Close - Open) + pixtemp = pixsCLBuffer; + pixsCLBuffer = pixdCLBuffer; + pixdCLBuffer = pixdCLIntermediate; + pixdCLIntermediate = pixtemp; - clStatus = pixSubtractCL_work(wpl, h, pixdCLBuffer, pixsCLBuffer); + clStatus = pixSubtractCL_work(wpl, h, pixdCLBuffer, pixsCLBuffer); - if (reqDataCopy) - { - //Read back output data from OCL buffer to cpu - pixd = mapOutputCLBuffer(rEnv, pixdCLBuffer, pixd, pixs, wpl*h, CL_MAP_READ); - } - return pixd; + if (reqDataCopy) { + // Read back output data from OCL buffer to cpu + pixd = + mapOutputCLBuffer(rEnv, pixdCLBuffer, pixd, pixs, wpl * h, CL_MAP_READ); + } + return pixd; } // OpenCL implementation of Get Lines from pix function //Note: Assumes the source and dest opencl buffer are initialized. No check done -void -OpenclDevice::pixGetLinesCL(PIX *pixd, - PIX *pixs, - PIX** pix_vline, - PIX** pix_hline, - PIX** pixClosed, - bool getpixClosed, - l_int32 close_hsize, l_int32 close_vsize, - l_int32 open_hsize, l_int32 open_vsize, - l_int32 line_hsize, l_int32 line_vsize) -{ - l_uint32 wpl, h; - cl_mem pixtemp; +void OpenclDevice::pixGetLinesCL(PIX *pixd, PIX *pixs, PIX **pix_vline, + PIX **pix_hline, PIX **pixClosed, + bool getpixClosed, l_int32 close_hsize, + l_int32 close_vsize, l_int32 open_hsize, + l_int32 open_vsize, l_int32 line_hsize, + l_int32 line_vsize) { + l_uint32 wpl, h; + cl_mem pixtemp; - wpl = pixGetWpl(pixs); - h = pixGetHeight(pixs); + wpl = pixGetWpl(pixs); + h = pixGetHeight(pixs); - //First step : Close Morph operation: Dilate followed by Erode - clStatus = pixCloseCL(close_hsize, close_vsize, wpl, h); + // First step : Close Morph operation: Dilate followed by Erode + clStatus = pixCloseCL(close_hsize, close_vsize, wpl, h); - //Copy the Close output to CPU buffer - if (getpixClosed) - { - *pixClosed = mapOutputCLBuffer(rEnv, pixdCLBuffer, *pixClosed, pixs, wpl*h, CL_MAP_READ, true, false); - } + // Copy the Close output to CPU buffer + if (getpixClosed) { + *pixClosed = mapOutputCLBuffer(rEnv, pixdCLBuffer, *pixClosed, pixs, + wpl * h, CL_MAP_READ, true, false); + } - //Store the output of close operation in an intermediate buffer - //this will be later used for pixsubtract - clStatus = clEnqueueCopyBuffer(rEnv.mpkCmdQueue, pixdCLBuffer, pixdCLIntermediate, 0, 0, sizeof(int) * wpl*h, 0, NULL, NULL); + // Store the output of close operation in an intermediate buffer + // this will be later used for pixsubtract + clStatus = + clEnqueueCopyBuffer(rEnv.mpkCmdQueue, pixdCLBuffer, pixdCLIntermediate, 0, + 0, sizeof(int) * wpl * h, 0, NULL, NULL); - //Second step: Open Operation - Erode followed by Dilate - pixtemp = pixsCLBuffer; - pixsCLBuffer = pixdCLBuffer; - pixdCLBuffer = pixtemp; + // Second step: Open Operation - Erode followed by Dilate + pixtemp = pixsCLBuffer; + pixsCLBuffer = pixdCLBuffer; + pixdCLBuffer = pixtemp; - clStatus = pixOpenCL(open_hsize, open_vsize, wpl, h); + clStatus = pixOpenCL(open_hsize, open_vsize, wpl, h); - //Third step: Subtract : (Close - Open) - pixtemp = pixsCLBuffer; - pixsCLBuffer = pixdCLBuffer; - pixdCLBuffer = pixdCLIntermediate; - pixdCLIntermediate = pixtemp; + // Third step: Subtract : (Close - Open) + pixtemp = pixsCLBuffer; + pixsCLBuffer = pixdCLBuffer; + pixdCLBuffer = pixdCLIntermediate; + pixdCLIntermediate = pixtemp; - clStatus = pixSubtractCL_work(wpl, h, pixdCLBuffer, pixsCLBuffer); + clStatus = pixSubtractCL_work(wpl, h, pixdCLBuffer, pixsCLBuffer); - //Store the output of Hollow operation in an intermediate buffer - //this will be later used - clStatus = clEnqueueCopyBuffer(rEnv.mpkCmdQueue, pixdCLBuffer, pixdCLIntermediate, 0, 0, sizeof(int) * wpl*h, 0, NULL, NULL); + // Store the output of Hollow operation in an intermediate buffer + // this will be later used + clStatus = + clEnqueueCopyBuffer(rEnv.mpkCmdQueue, pixdCLBuffer, pixdCLIntermediate, 0, + 0, sizeof(int) * wpl * h, 0, NULL, NULL); - pixtemp = pixsCLBuffer; - pixsCLBuffer = pixdCLBuffer; - pixdCLBuffer = pixtemp; + pixtemp = pixsCLBuffer; + pixsCLBuffer = pixdCLBuffer; + pixdCLBuffer = pixtemp; - //Fourth step: Get vertical line - //pixOpenBrick(NULL, pix_hollow, 1, min_line_length); - clStatus = pixOpenCL(1, line_vsize, wpl, h); + // Fourth step: Get vertical line + // pixOpenBrick(NULL, pix_hollow, 1, min_line_length); + clStatus = pixOpenCL(1, line_vsize, wpl, h); - //Copy the vertical line output to CPU buffer - *pix_vline = mapOutputCLBuffer(rEnv, pixdCLBuffer, *pix_vline, pixs, wpl*h, CL_MAP_READ, true, false); + // Copy the vertical line output to CPU buffer + *pix_vline = mapOutputCLBuffer(rEnv, pixdCLBuffer, *pix_vline, pixs, wpl * h, + CL_MAP_READ, true, false); - pixtemp = pixsCLBuffer; - pixsCLBuffer = pixdCLIntermediate; - pixdCLIntermediate = pixtemp; + pixtemp = pixsCLBuffer; + pixsCLBuffer = pixdCLIntermediate; + pixdCLIntermediate = pixtemp; - //Fifth step: Get horizontal line - //pixOpenBrick(NULL, pix_hollow, min_line_length, 1); - clStatus = pixOpenCL(line_hsize, 1, wpl, h); + // Fifth step: Get horizontal line + // pixOpenBrick(NULL, pix_hollow, min_line_length, 1); + clStatus = pixOpenCL(line_hsize, 1, wpl, h); - //Copy the horizontal line output to CPU buffer - *pix_hline = mapOutputCLBuffer(rEnv, pixdCLBuffer, *pix_hline, pixs, wpl*h, CL_MAP_READ, true, true); + // Copy the horizontal line output to CPU buffer + *pix_hline = mapOutputCLBuffer(rEnv, pixdCLBuffer, *pix_hline, pixs, wpl * h, + CL_MAP_READ, true, true); - return; + return; } - /************************************************************************* * HistogramRect * Otsu Thresholding Operations * histogramAllChannels is laid out as all channel 0, then all channel 1... * only supports 1 or 4 channels (bytes_per_pixel) ************************************************************************/ -int OpenclDevice::HistogramRectOCL( - const unsigned char* imageData, - int bytes_per_pixel, - int bytes_per_line, - int left, // always 0 - int top, // always 0 - int width, - int height, - int kHistogramSize, - int* histogramAllChannels) -{ -PERF_COUNT_START("HistogramRectOCL") - cl_int clStatus; - int retVal= 0; - KernelEnv histKern; - SetKernelEnv( &histKern ); - KernelEnv histRedKern; - SetKernelEnv( &histRedKern ); - /* map imagedata to device as read only */ - // USE_HOST_PTR uses onion+ bus which is slowest option; also happens to be coherent which we don't need. - // faster option would be to allocate initial image buffer - // using a garlic bus memory type - cl_mem imageBuffer = clCreateBuffer( histKern.mpkContext, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, width*height*bytes_per_pixel*sizeof(char), (void *)imageData, &clStatus ); - CHECK_OPENCL( clStatus, "clCreateBuffer imageBuffer"); +int OpenclDevice::HistogramRectOCL(const unsigned char *imageData, + int bytes_per_pixel, int bytes_per_line, + int left, // always 0 + int top, // always 0 + int width, int height, int kHistogramSize, + int *histogramAllChannels) { + PERF_COUNT_START("HistogramRectOCL") + cl_int clStatus; + int retVal = 0; + KernelEnv histKern; + SetKernelEnv(&histKern); + KernelEnv histRedKern; + SetKernelEnv(&histRedKern); + /* map imagedata to device as read only */ + // USE_HOST_PTR uses onion+ bus which is slowest option; also happens to be + // coherent which we don't need. + // faster option would be to allocate initial image buffer + // using a garlic bus memory type + cl_mem imageBuffer = clCreateBuffer( + histKern.mpkContext, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, + width * height * bytes_per_pixel * sizeof(char), (void *)imageData, + &clStatus); + CHECK_OPENCL(clStatus, "clCreateBuffer imageBuffer"); - /* setup work group size parameters */ - int block_size = 256; - cl_uint numCUs; - clStatus = clGetDeviceInfo( gpuEnv.mpDevID, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(numCUs), &numCUs, NULL); - CHECK_OPENCL( clStatus, "clCreateBuffer imageBuffer"); + /* setup work group size parameters */ + int block_size = 256; + cl_uint numCUs; + clStatus = clGetDeviceInfo(gpuEnv.mpDevID, CL_DEVICE_MAX_COMPUTE_UNITS, + sizeof(numCUs), &numCUs, NULL); + CHECK_OPENCL(clStatus, "clCreateBuffer imageBuffer"); - int requestedOccupancy = 10; - int numWorkGroups = numCUs * requestedOccupancy; - int numThreads = block_size*numWorkGroups; - size_t local_work_size[] = {static_cast(block_size)}; - size_t global_work_size[] = {static_cast(numThreads)}; - size_t red_global_work_size[] = {static_cast(block_size*kHistogramSize*bytes_per_pixel)}; + int requestedOccupancy = 10; + int numWorkGroups = numCUs * requestedOccupancy; + int numThreads = block_size * numWorkGroups; + size_t local_work_size[] = {static_cast(block_size)}; + size_t global_work_size[] = {static_cast(numThreads)}; + size_t red_global_work_size[] = { + static_cast(block_size * kHistogramSize * bytes_per_pixel)}; - /* map histogramAllChannels as write only */ - int numBins = kHistogramSize*bytes_per_pixel*numWorkGroups; + /* map histogramAllChannels as write only */ + int numBins = kHistogramSize * bytes_per_pixel * numWorkGroups; - cl_mem histogramBuffer = clCreateBuffer( histKern.mpkContext, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, kHistogramSize*bytes_per_pixel*sizeof(int), (void *)histogramAllChannels, &clStatus ); - CHECK_OPENCL( clStatus, "clCreateBuffer histogramBuffer"); + cl_mem histogramBuffer = clCreateBuffer( + histKern.mpkContext, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, + kHistogramSize * bytes_per_pixel * sizeof(int), + (void *)histogramAllChannels, &clStatus); + CHECK_OPENCL(clStatus, "clCreateBuffer histogramBuffer"); - /* intermediate histogram buffer */ - int histRed = 256; - int tmpHistogramBins = kHistogramSize*bytes_per_pixel*histRed; + /* intermediate histogram buffer */ + int histRed = 256; + int tmpHistogramBins = kHistogramSize * bytes_per_pixel * histRed; - cl_mem tmpHistogramBuffer = clCreateBuffer( histKern.mpkContext, CL_MEM_READ_WRITE, tmpHistogramBins*sizeof(cl_uint), NULL, &clStatus ); - CHECK_OPENCL( clStatus, "clCreateBuffer tmpHistogramBuffer"); + cl_mem tmpHistogramBuffer = + clCreateBuffer(histKern.mpkContext, CL_MEM_READ_WRITE, + tmpHistogramBins * sizeof(cl_uint), NULL, &clStatus); + CHECK_OPENCL(clStatus, "clCreateBuffer tmpHistogramBuffer"); - /* atomic sync buffer */ - int *zeroBuffer = new int[1]; - zeroBuffer[0] = 0; - cl_mem atomicSyncBuffer = clCreateBuffer( histKern.mpkContext, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, sizeof(cl_int), (void *)zeroBuffer, &clStatus ); - CHECK_OPENCL( clStatus, "clCreateBuffer atomicSyncBuffer"); - delete[] zeroBuffer; - //Create kernel objects based on bytes_per_pixel - if (bytes_per_pixel == 1) - { - histKern.mpkKernel = clCreateKernel( histKern.mpkProgram, "kernel_HistogramRectOneChannel", &clStatus ); - CHECK_OPENCL( clStatus, "clCreateKernel kernel_HistogramRectOneChannel"); + /* atomic sync buffer */ + int *zeroBuffer = new int[1]; + zeroBuffer[0] = 0; + cl_mem atomicSyncBuffer = clCreateBuffer( + histKern.mpkContext, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, + sizeof(cl_int), (void *)zeroBuffer, &clStatus); + CHECK_OPENCL(clStatus, "clCreateBuffer atomicSyncBuffer"); + delete[] zeroBuffer; + // Create kernel objects based on bytes_per_pixel + if (bytes_per_pixel == 1) { + histKern.mpkKernel = clCreateKernel( + histKern.mpkProgram, "kernel_HistogramRectOneChannel", &clStatus); + CHECK_OPENCL(clStatus, "clCreateKernel kernel_HistogramRectOneChannel"); - histRedKern.mpkKernel = clCreateKernel( histRedKern.mpkProgram, "kernel_HistogramRectOneChannelReduction", &clStatus ); - CHECK_OPENCL( clStatus, "clCreateKernel kernel_HistogramRectOneChannelReduction"); - } else { + histRedKern.mpkKernel = + clCreateKernel(histRedKern.mpkProgram, + "kernel_HistogramRectOneChannelReduction", &clStatus); + CHECK_OPENCL(clStatus, + "clCreateKernel kernel_HistogramRectOneChannelReduction"); + } else { histKern.mpkKernel = clCreateKernel( histKern.mpkProgram, "kernel_HistogramRectAllChannels", &clStatus ); CHECK_OPENCL( clStatus, "clCreateKernel kernel_HistogramRectAllChannels"); @@ -2524,10 +2351,9 @@ PERF_COUNT_SUB("before") 0, NULL, NULL ); CHECK_OPENCL( clStatus, "clEnqueueNDRangeKernel kernel_HistogramRectAllChannels" ); clFinish( histKern.mpkCmdQueue ); - if(clStatus !=0) - { - retVal = -1; - } + if (clStatus != 0) { + retVal = -1; + } /* launch histogram */ clStatus = clEnqueueNDRangeKernel( histRedKern.mpkCmdQueue, @@ -2536,27 +2362,24 @@ PERF_COUNT_SUB("before") 0, NULL, NULL ); CHECK_OPENCL( clStatus, "clEnqueueNDRangeKernel kernel_HistogramRectAllChannelsReduction" ); clFinish( histRedKern.mpkCmdQueue ); - if(clStatus !=0) - { - retVal = -1; - } -PERF_COUNT_SUB("redKernel") + if (clStatus != 0) { + retVal = -1; + } + PERF_COUNT_SUB("redKernel") /* map results back from gpu */ ptr = clEnqueueMapBuffer(histRedKern.mpkCmdQueue, histogramBuffer, CL_TRUE, CL_MAP_READ, 0, kHistogramSize*bytes_per_pixel*sizeof(int), 0, NULL, NULL, &clStatus); CHECK_OPENCL( clStatus, "clEnqueueMapBuffer histogramBuffer"); - if(clStatus !=0) - { - retVal = -1; - } + if (clStatus != 0) { + retVal = -1; + } clEnqueueUnmapMemObject(histRedKern.mpkCmdQueue, histogramBuffer, ptr, 0, NULL, NULL); clReleaseMemObject(histogramBuffer); clReleaseMemObject(imageBuffer); PERF_COUNT_SUB("after") PERF_COUNT_END - return retVal; - +return retVal; } /************************************************************************* @@ -2564,111 +2387,122 @@ PERF_COUNT_END * from the class, using thresholds/hi_values to the output IMAGE. * only supports 1 or 4 channels ************************************************************************/ -int OpenclDevice::ThresholdRectToPixOCL( - const unsigned char* imageData, - int bytes_per_pixel, - int bytes_per_line, - const int* thresholds, - const int* hi_values, - Pix** pix, - int height, - int width, - int top, - int left) { -PERF_COUNT_START("ThresholdRectToPixOCL") - int retVal =0; - /* create pix result buffer */ - *pix = pixCreate(width, height, 1); - uinT32* pixData = pixGetData(*pix); - int wpl = pixGetWpl(*pix); - int pixSize = wpl*height*sizeof(uinT32); // number of pixels +int OpenclDevice::ThresholdRectToPixOCL(const unsigned char *imageData, + int bytes_per_pixel, int bytes_per_line, + const int *thresholds, + const int *hi_values, Pix **pix, + int height, int width, int top, + int left) { + PERF_COUNT_START("ThresholdRectToPixOCL") + int retVal = 0; + /* create pix result buffer */ + *pix = pixCreate(width, height, 1); + uinT32 *pixData = pixGetData(*pix); + int wpl = pixGetWpl(*pix); + int pixSize = wpl * height * sizeof(uinT32); // number of pixels - cl_int clStatus; - KernelEnv rEnv; - SetKernelEnv( &rEnv ); + cl_int clStatus; + KernelEnv rEnv; + SetKernelEnv(&rEnv); - /* setup work group size parameters */ - int block_size = 256; - cl_uint numCUs = 6; - clStatus = clGetDeviceInfo( gpuEnv.mpDevID, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(numCUs), &numCUs, NULL); - CHECK_OPENCL( clStatus, "clCreateBuffer imageBuffer"); + /* setup work group size parameters */ + int block_size = 256; + cl_uint numCUs = 6; + clStatus = clGetDeviceInfo(gpuEnv.mpDevID, CL_DEVICE_MAX_COMPUTE_UNITS, + sizeof(numCUs), &numCUs, NULL); + CHECK_OPENCL(clStatus, "clCreateBuffer imageBuffer"); - int requestedOccupancy = 10; - int numWorkGroups = numCUs * requestedOccupancy; - int numThreads = block_size*numWorkGroups; - size_t local_work_size[] = {(size_t) block_size}; - size_t global_work_size[] = {(size_t) numThreads}; + int requestedOccupancy = 10; + int numWorkGroups = numCUs * requestedOccupancy; + int numThreads = block_size * numWorkGroups; + size_t local_work_size[] = {(size_t)block_size}; + size_t global_work_size[] = {(size_t)numThreads}; - /* map imagedata to device as read only */ - // USE_HOST_PTR uses onion+ bus which is slowest option; also happens to be coherent which we don't need. - // faster option would be to allocate initial image buffer - // using a garlic bus memory type - cl_mem imageBuffer = clCreateBuffer( rEnv.mpkContext, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, width*height*bytes_per_pixel*sizeof(char), (void *)imageData, &clStatus ); - CHECK_OPENCL( clStatus, "clCreateBuffer imageBuffer"); + /* map imagedata to device as read only */ + // USE_HOST_PTR uses onion+ bus which is slowest option; also happens to be + // coherent which we don't need. + // faster option would be to allocate initial image buffer + // using a garlic bus memory type + cl_mem imageBuffer = + clCreateBuffer(rEnv.mpkContext, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, + width * height * bytes_per_pixel * sizeof(char), + (void *)imageData, &clStatus); + CHECK_OPENCL(clStatus, "clCreateBuffer imageBuffer"); - /* map pix as write only */ - pixThBuffer = clCreateBuffer( rEnv.mpkContext, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, pixSize, (void *)pixData, &clStatus ); - CHECK_OPENCL( clStatus, "clCreateBuffer pix"); + /* map pix as write only */ + pixThBuffer = + clCreateBuffer(rEnv.mpkContext, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, + pixSize, (void *)pixData, &clStatus); + CHECK_OPENCL(clStatus, "clCreateBuffer pix"); - /* map thresholds and hi_values */ - cl_mem thresholdsBuffer = clCreateBuffer( rEnv.mpkContext, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, bytes_per_pixel*sizeof(int), (void *)thresholds, &clStatus ); - CHECK_OPENCL( clStatus, "clCreateBuffer thresholdBuffer"); - cl_mem hiValuesBuffer = clCreateBuffer( rEnv.mpkContext, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, bytes_per_pixel*sizeof(int), (void *)hi_values, &clStatus ); - CHECK_OPENCL( clStatus, "clCreateBuffer hiValuesBuffer"); + /* map thresholds and hi_values */ + cl_mem thresholdsBuffer = clCreateBuffer( + rEnv.mpkContext, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, + bytes_per_pixel * sizeof(int), (void *)thresholds, &clStatus); + CHECK_OPENCL(clStatus, "clCreateBuffer thresholdBuffer"); + cl_mem hiValuesBuffer = clCreateBuffer( + rEnv.mpkContext, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, + bytes_per_pixel * sizeof(int), (void *)hi_values, &clStatus); + CHECK_OPENCL(clStatus, "clCreateBuffer hiValuesBuffer"); - /* compile kernel */ - if (bytes_per_pixel == 4) { - rEnv.mpkKernel = clCreateKernel( rEnv.mpkProgram, "kernel_ThresholdRectToPix", &clStatus ); - CHECK_OPENCL( clStatus, "clCreateKernel kernel_ThresholdRectToPix"); - } else { - rEnv.mpkKernel = clCreateKernel( rEnv.mpkProgram, "kernel_ThresholdRectToPix_OneChan", &clStatus ); - CHECK_OPENCL( clStatus, "clCreateKernel kernel_ThresholdRectToPix_OneChan"); - } + /* compile kernel */ + if (bytes_per_pixel == 4) { + rEnv.mpkKernel = + clCreateKernel(rEnv.mpkProgram, "kernel_ThresholdRectToPix", &clStatus); + CHECK_OPENCL(clStatus, "clCreateKernel kernel_ThresholdRectToPix"); + } else { + rEnv.mpkKernel = clCreateKernel( + rEnv.mpkProgram, "kernel_ThresholdRectToPix_OneChan", &clStatus); + CHECK_OPENCL(clStatus, "clCreateKernel kernel_ThresholdRectToPix_OneChan"); + } - /* set kernel arguments */ - clStatus = clSetKernelArg( rEnv.mpkKernel, 0, sizeof(cl_mem), (void *)&imageBuffer ); - CHECK_OPENCL( clStatus, "clSetKernelArg imageBuffer"); - cl_uint numPixels = width*height; - clStatus = clSetKernelArg( rEnv.mpkKernel, 1, sizeof(int), (void *)&height ); - CHECK_OPENCL( clStatus, "clSetKernelArg height" ); - clStatus = clSetKernelArg( rEnv.mpkKernel, 2, sizeof(int), (void *)&width ); - CHECK_OPENCL( clStatus, "clSetKernelArg width" ); - clStatus = clSetKernelArg( rEnv.mpkKernel, 3, sizeof(int), (void *)&wpl ); - CHECK_OPENCL( clStatus, "clSetKernelArg wpl" ); - clStatus = clSetKernelArg( rEnv.mpkKernel, 4, sizeof(cl_mem), (void *)&thresholdsBuffer ); - CHECK_OPENCL( clStatus, "clSetKernelArg thresholdsBuffer" ); - clStatus = clSetKernelArg( rEnv.mpkKernel, 5, sizeof(cl_mem), (void *)&hiValuesBuffer ); - CHECK_OPENCL( clStatus, "clSetKernelArg hiValuesBuffer" ); - clStatus = clSetKernelArg( rEnv.mpkKernel, 6, sizeof(cl_mem), (void *)&pixThBuffer ); - CHECK_OPENCL( clStatus, "clSetKernelArg pixThBuffer"); + /* set kernel arguments */ + clStatus = + clSetKernelArg(rEnv.mpkKernel, 0, sizeof(cl_mem), (void *)&imageBuffer); + CHECK_OPENCL(clStatus, "clSetKernelArg imageBuffer"); + cl_uint numPixels = width * height; + clStatus = clSetKernelArg(rEnv.mpkKernel, 1, sizeof(int), (void *)&height); + CHECK_OPENCL(clStatus, "clSetKernelArg height"); + clStatus = clSetKernelArg(rEnv.mpkKernel, 2, sizeof(int), (void *)&width); + CHECK_OPENCL(clStatus, "clSetKernelArg width"); + clStatus = clSetKernelArg(rEnv.mpkKernel, 3, sizeof(int), (void *)&wpl); + CHECK_OPENCL(clStatus, "clSetKernelArg wpl"); + clStatus = clSetKernelArg(rEnv.mpkKernel, 4, sizeof(cl_mem), + (void *)&thresholdsBuffer); + CHECK_OPENCL(clStatus, "clSetKernelArg thresholdsBuffer"); + clStatus = clSetKernelArg(rEnv.mpkKernel, 5, sizeof(cl_mem), + (void *)&hiValuesBuffer); + CHECK_OPENCL(clStatus, "clSetKernelArg hiValuesBuffer"); + clStatus = + clSetKernelArg(rEnv.mpkKernel, 6, sizeof(cl_mem), (void *)&pixThBuffer); + CHECK_OPENCL(clStatus, "clSetKernelArg pixThBuffer"); - /* launch kernel & wait */ -PERF_COUNT_SUB("before") - clStatus = clEnqueueNDRangeKernel( - rEnv.mpkCmdQueue, - rEnv.mpkKernel, - 1, NULL, global_work_size, local_work_size, - 0, NULL, NULL ); - CHECK_OPENCL( clStatus, "clEnqueueNDRangeKernel kernel_ThresholdRectToPix" ); - clFinish( rEnv.mpkCmdQueue ); -PERF_COUNT_SUB("kernel") - if(clStatus !=0) - { - printf("Setting return value to -1\n"); - retVal = -1; - } - /* map results back from gpu */ - void *ptr = clEnqueueMapBuffer(rEnv.mpkCmdQueue, pixThBuffer, CL_TRUE, CL_MAP_READ, 0, pixSize, 0, NULL, NULL, &clStatus); - CHECK_OPENCL( clStatus, "clEnqueueMapBuffer histogramBuffer"); - clEnqueueUnmapMemObject(rEnv.mpkCmdQueue, pixThBuffer, ptr, 0, NULL, NULL); + /* launch kernel & wait */ + PERF_COUNT_SUB("before") + clStatus = + clEnqueueNDRangeKernel(rEnv.mpkCmdQueue, rEnv.mpkKernel, 1, NULL, + global_work_size, local_work_size, 0, NULL, NULL); + CHECK_OPENCL(clStatus, "clEnqueueNDRangeKernel kernel_ThresholdRectToPix"); + clFinish(rEnv.mpkCmdQueue); + PERF_COUNT_SUB("kernel") + if (clStatus != 0) { + printf("Setting return value to -1\n"); + retVal = -1; + } + /* map results back from gpu */ + void *ptr = + clEnqueueMapBuffer(rEnv.mpkCmdQueue, pixThBuffer, CL_TRUE, CL_MAP_READ, 0, + pixSize, 0, NULL, NULL, &clStatus); + CHECK_OPENCL(clStatus, "clEnqueueMapBuffer histogramBuffer"); + clEnqueueUnmapMemObject(rEnv.mpkCmdQueue, pixThBuffer, ptr, 0, NULL, NULL); - clReleaseMemObject(imageBuffer); - clReleaseMemObject(thresholdsBuffer); - clReleaseMemObject(hiValuesBuffer); + clReleaseMemObject(imageBuffer); + clReleaseMemObject(thresholdsBuffer); + clReleaseMemObject(hiValuesBuffer); -PERF_COUNT_SUB("after") -PERF_COUNT_END -return retVal; + PERF_COUNT_SUB("after") + PERF_COUNT_END + return retVal; } @@ -2745,7 +2579,6 @@ void populateTessScoreEvaluationInputData( TessScoreEvaluationInputData *input ) float fractionBlack = 0.1; // how much of the image should be blackened int numSpots = (height*width)*fractionBlack/(maxLineWidth*maxLineWidth/2/2); for (int i = 0; i < numSpots; i++) { - int lineWidth = rand()%maxLineWidth; int col = lineWidth + rand()%(width-2*lineWidth); int row = lineWidth + rand()%(height-2*lineWidth); @@ -2776,15 +2609,14 @@ typedef struct _TessDeviceScore { *****************************************************************************/ double composeRGBPixelMicroBench( GPUEnv *env, TessScoreEvaluationInputData input, ds_device_type type ) { - double time = 0; #if ON_WINDOWS LARGE_INTEGER freq, time_funct_start, time_funct_end; QueryPerformanceFrequency(&freq); #elif ON_APPLE - mach_timebase_info_data_t info = { 0, 0 }; + mach_timebase_info_data_t info = {0, 0}; mach_timebase_info(&info); - long long start,stop; + long long start, stop; #else timespec time_funct_start, time_funct_end; #endif @@ -2795,8 +2627,8 @@ double composeRGBPixelMicroBench( GPUEnv *env, TessScoreEvaluationInputData inpu if (type == DS_DEVICE_OPENCL_DEVICE) { #if ON_WINDOWS QueryPerformanceCounter(&time_funct_start); -#elif ON_APPLE - start = mach_absolute_time(); +#elif ON_APPLE + start = mach_absolute_time(); #else clock_gettime( CLOCK_MONOTONIC, &time_funct_start ); #endif @@ -2807,9 +2639,9 @@ double composeRGBPixelMicroBench( GPUEnv *env, TessScoreEvaluationInputData inpu #if ON_WINDOWS QueryPerformanceCounter(&time_funct_end); time = (time_funct_end.QuadPart-time_funct_start.QuadPart)/(double)(freq.QuadPart); -#elif ON_APPLE - stop = mach_absolute_time(); - time = ((stop - start) * (double) info.numer / info.denom) / 1.0E9; +#elif ON_APPLE + stop = mach_absolute_time(); + time = ((stop - start) * (double)info.numer / info.denom) / 1.0E9; #else clock_gettime( CLOCK_MONOTONIC, &time_funct_end ); time = (time_funct_end.tv_sec - time_funct_start.tv_sec)*1.0 + (time_funct_end.tv_nsec - time_funct_start.tv_nsec)/1000000000.0; @@ -2818,8 +2650,8 @@ double composeRGBPixelMicroBench( GPUEnv *env, TessScoreEvaluationInputData inpu } else { #if ON_WINDOWS QueryPerformanceCounter(&time_funct_start); -#elif ON_APPLE - start = mach_absolute_time(); +#elif ON_APPLE + start = mach_absolute_time(); #else clock_gettime( CLOCK_MONOTONIC, &time_funct_start ); #endif @@ -2832,7 +2664,6 @@ double composeRGBPixelMicroBench( GPUEnv *env, TessScoreEvaluationInputData inpu int idx = 0; for (i = 0; i < input.height ; i++) { for (j = 0; j < input.width; j++) { - l_uint32 tiffword = tiffdata[i * input.width + j]; l_int32 rval = ((tiffword) & 0xff); l_int32 gval = (((tiffword) >> 8) & 0xff); @@ -2845,9 +2676,9 @@ double composeRGBPixelMicroBench( GPUEnv *env, TessScoreEvaluationInputData inpu #if ON_WINDOWS QueryPerformanceCounter(&time_funct_end); time = (time_funct_end.QuadPart-time_funct_start.QuadPart)/(double)(freq.QuadPart); -#elif ON_APPLE - stop = mach_absolute_time(); - time = ((stop - start) * (double) info.numer / info.denom) / 1.0E9; +#elif ON_APPLE + stop = mach_absolute_time(); + time = ((stop - start) * (double)info.numer / info.denom) / 1.0E9; #else clock_gettime( CLOCK_MONOTONIC, &time_funct_end ); time = (time_funct_end.tv_sec - time_funct_start.tv_sec)*1.0 + (time_funct_end.tv_nsec - time_funct_start.tv_nsec)/1000000000.0; @@ -2862,15 +2693,14 @@ double composeRGBPixelMicroBench( GPUEnv *env, TessScoreEvaluationInputData inpu } double histogramRectMicroBench( GPUEnv *env, TessScoreEvaluationInputData input, ds_device_type type ) { - double time; #if ON_WINDOWS LARGE_INTEGER freq, time_funct_start, time_funct_end; QueryPerformanceFrequency(&freq); -#elif ON_APPLE - mach_timebase_info_data_t info = { 0, 0 }; +#elif ON_APPLE + mach_timebase_info_data_t info = {0, 0}; mach_timebase_info(&info); - long long start,stop; + long long start, stop; #else timespec time_funct_start, time_funct_end; #endif @@ -2882,58 +2712,56 @@ double histogramRectMicroBench( GPUEnv *env, TessScoreEvaluationInputData input, int kHistogramSize = 256; int bytes_per_line = input.width*input.numChannels; int *histogramAllChannels = new int[kHistogramSize*input.numChannels]; - int retVal= 0; + int retVal = 0; // function call if (type == DS_DEVICE_OPENCL_DEVICE) { #if ON_WINDOWS QueryPerformanceCounter(&time_funct_start); -#elif ON_APPLE - start = mach_absolute_time(); +#elif ON_APPLE + start = mach_absolute_time(); #else clock_gettime( CLOCK_MONOTONIC, &time_funct_start ); #endif OpenclDevice::gpuEnv = *env; int wpl = pixGetWpl(input.pix); - retVal= OpenclDevice::HistogramRectOCL(input.imageData, input.numChannels, bytes_per_line, top, left, input.width, input.height, kHistogramSize, histogramAllChannels); + retVal = OpenclDevice::HistogramRectOCL( + input.imageData, input.numChannels, bytes_per_line, top, left, + input.width, input.height, kHistogramSize, histogramAllChannels); #if ON_WINDOWS QueryPerformanceCounter(&time_funct_end); time = (time_funct_end.QuadPart-time_funct_start.QuadPart)/(double)(freq.QuadPart); -#elif ON_APPLE - stop = mach_absolute_time(); - if(retVal ==0) - { - time = ((stop - start) * (double) info.numer / info.denom) / 1.0E9; - } - else - { - time= FLT_MAX; - } +#elif ON_APPLE + stop = mach_absolute_time(); + if (retVal == 0) { + time = ((stop - start) * (double)info.numer / info.denom) / 1.0E9; + } else { + time = FLT_MAX; + } #else clock_gettime( CLOCK_MONOTONIC, &time_funct_end ); time = (time_funct_end.tv_sec - time_funct_start.tv_sec)*1.0 + (time_funct_end.tv_nsec - time_funct_start.tv_nsec)/1000000000.0; #endif } else { - int *histogram = new int[kHistogramSize]; #if ON_WINDOWS QueryPerformanceCounter(&time_funct_start); -#elif ON_APPLE - start = mach_absolute_time(); +#elif ON_APPLE + start = mach_absolute_time(); #else clock_gettime( CLOCK_MONOTONIC, &time_funct_start ); #endif for (int ch = 0; ch < input.numChannels; ++ch) { - tesseract::HistogramRect(input.pix, input.numChannels, - left, top, input.width, input.height, histogram); + tesseract::HistogramRect(input.pix, input.numChannels, left, top, + input.width, input.height, histogram); } #if ON_WINDOWS QueryPerformanceCounter(&time_funct_end); time = (time_funct_end.QuadPart-time_funct_start.QuadPart)/(double)(freq.QuadPart); -#elif ON_APPLE - stop = mach_absolute_time(); - time = ((stop - start) * (double) info.numer / info.denom) / 1.0E9; +#elif ON_APPLE + stop = mach_absolute_time(); + time = ((stop - start) * (double)info.numer / info.denom) / 1.0E9; #else clock_gettime( CLOCK_MONOTONIC, &time_funct_end ); time = (time_funct_end.tv_sec - time_funct_start.tv_sec)*1.0 + (time_funct_end.tv_nsec - time_funct_start.tv_nsec)/1000000000.0; @@ -2985,16 +2813,15 @@ void ThresholdRectToPix_Native(const unsigned char* imagedata, } double thresholdRectToPixMicroBench( GPUEnv *env, TessScoreEvaluationInputData input, ds_device_type type ) { - double time; - int retVal =0; + int retVal = 0; #if ON_WINDOWS LARGE_INTEGER freq, time_funct_start, time_funct_end; QueryPerformanceFrequency(&freq); -#elif ON_APPLE - mach_timebase_info_data_t info = { 0, 0 }; +#elif ON_APPLE + mach_timebase_info_data_t info = {0, 0}; mach_timebase_info(&info); - long long start,stop; + long long start, stop; #else timespec time_funct_start, time_funct_end; #endif @@ -3020,29 +2847,29 @@ double thresholdRectToPixMicroBench( GPUEnv *env, TessScoreEvaluationInputData i if (type == DS_DEVICE_OPENCL_DEVICE) { #if ON_WINDOWS QueryPerformanceCounter(&time_funct_start); -#elif ON_APPLE - start = mach_absolute_time(); +#elif ON_APPLE + start = mach_absolute_time(); #else clock_gettime( CLOCK_MONOTONIC, &time_funct_start ); #endif OpenclDevice::gpuEnv = *env; int wpl = pixGetWpl(input.pix); - retVal= OpenclDevice::ThresholdRectToPixOCL(input.imageData, input.numChannels, bytes_per_line, thresholds, hi_values, &input.pix, input.height, input.width, top, left); + retVal = OpenclDevice::ThresholdRectToPixOCL( + input.imageData, input.numChannels, bytes_per_line, thresholds, + hi_values, &input.pix, input.height, input.width, top, left); #if ON_WINDOWS QueryPerformanceCounter(&time_funct_end); time = (time_funct_end.QuadPart-time_funct_start.QuadPart)/(double)(freq.QuadPart); -#elif ON_APPLE - stop = mach_absolute_time(); - if(retVal ==0) - { - time = ((stop - start) * (double) info.numer / info.denom) / 1.0E9;; - } - else - { - time= FLT_MAX; - } +#elif ON_APPLE + stop = mach_absolute_time(); + if (retVal == 0) { + time = ((stop - start) * (double)info.numer / info.denom) / 1.0E9; + ; + } else { + time = FLT_MAX; + } #else clock_gettime( CLOCK_MONOTONIC, &time_funct_end ); @@ -3055,8 +2882,8 @@ double thresholdRectToPixMicroBench( GPUEnv *env, TessScoreEvaluationInputData i thresholder.SetImage( input.pix ); #if ON_WINDOWS QueryPerformanceCounter(&time_funct_start); -#elif ON_APPLE - start = mach_absolute_time(); +#elif ON_APPLE + start = mach_absolute_time(); #else clock_gettime( CLOCK_MONOTONIC, &time_funct_start ); #endif @@ -3066,9 +2893,9 @@ double thresholdRectToPixMicroBench( GPUEnv *env, TessScoreEvaluationInputData i #if ON_WINDOWS QueryPerformanceCounter(&time_funct_end); time = (time_funct_end.QuadPart-time_funct_start.QuadPart)/(double)(freq.QuadPart); -#elif ON_APPLE - stop = mach_absolute_time(); - time = ((stop - start) * (double) info.numer / info.denom) / 1.0E9; +#elif ON_APPLE + stop = mach_absolute_time(); + time = ((stop - start) * (double)info.numer / info.denom) / 1.0E9; #else clock_gettime( CLOCK_MONOTONIC, &time_funct_end ); time = (time_funct_end.tv_sec - time_funct_start.tv_sec)*1.0 + (time_funct_end.tv_nsec - time_funct_start.tv_nsec)/1000000000.0; @@ -3087,10 +2914,10 @@ double getLineMasksMorphMicroBench( GPUEnv *env, TessScoreEvaluationInputData in #if ON_WINDOWS LARGE_INTEGER freq, time_funct_start, time_funct_end; QueryPerformanceFrequency(&freq); -#elif ON_APPLE - mach_timebase_info_data_t info = { 0, 0 }; +#elif ON_APPLE + mach_timebase_info_data_t info = {0, 0}; mach_timebase_info(&info); - long long start,stop; + long long start, stop; #else timespec time_funct_start, time_funct_end; #endif @@ -3108,8 +2935,8 @@ double getLineMasksMorphMicroBench( GPUEnv *env, TessScoreEvaluationInputData in if (type == DS_DEVICE_OPENCL_DEVICE) { #if ON_WINDOWS QueryPerformanceCounter(&time_funct_start); -#elif ON_APPLE - start = mach_absolute_time(); +#elif ON_APPLE + start = mach_absolute_time(); #else clock_gettime( CLOCK_MONOTONIC, &time_funct_start ); #endif @@ -3124,9 +2951,9 @@ double getLineMasksMorphMicroBench( GPUEnv *env, TessScoreEvaluationInputData in #if ON_WINDOWS QueryPerformanceCounter(&time_funct_end); time = (time_funct_end.QuadPart-time_funct_start.QuadPart)/(double)(freq.QuadPart); -#elif ON_APPLE - stop = mach_absolute_time(); - time = ((stop - start) * (double) info.numer / info.denom) / 1.0E9; +#elif ON_APPLE + stop = mach_absolute_time(); + time = ((stop - start) * (double)info.numer / info.denom) / 1.0E9; #else clock_gettime( CLOCK_MONOTONIC, &time_funct_end ); time = (time_funct_end.tv_sec - time_funct_start.tv_sec)*1.0 + (time_funct_end.tv_nsec - time_funct_start.tv_nsec)/1000000000.0; @@ -3134,8 +2961,8 @@ double getLineMasksMorphMicroBench( GPUEnv *env, TessScoreEvaluationInputData in } else { #if ON_WINDOWS QueryPerformanceCounter(&time_funct_start); -#elif ON_APPLE - start = mach_absolute_time(); +#elif ON_APPLE + start = mach_absolute_time(); #else clock_gettime( CLOCK_MONOTONIC, &time_funct_start ); #endif @@ -3153,9 +2980,9 @@ double getLineMasksMorphMicroBench( GPUEnv *env, TessScoreEvaluationInputData in #if ON_WINDOWS QueryPerformanceCounter(&time_funct_end); time = (time_funct_end.QuadPart-time_funct_start.QuadPart)/(double)(freq.QuadPart); -#elif ON_APPLE - stop = mach_absolute_time(); - time = ((stop - start) * (double) info.numer / info.denom) / 1.0E9; +#elif ON_APPLE + stop = mach_absolute_time(); + time = ((stop - start) * (double)info.numer / info.denom) / 1.0E9; #else clock_gettime( CLOCK_MONOTONIC, &time_funct_end ); time = (time_funct_end.tv_sec - time_funct_start.tv_sec)*1.0 + (time_funct_end.tv_nsec - time_funct_start.tv_nsec)/1000000000.0; @@ -3173,7 +3000,6 @@ double getLineMasksMorphMicroBench( GPUEnv *env, TessScoreEvaluationInputData in #include "stdlib.h" - // encode score object as byte string ds_status serializeScore( ds_device* device, void **serializedScore, unsigned int* serializedScoreSize ) { *serializedScoreSize = sizeof(TessDeviceScore); @@ -3190,14 +3016,13 @@ ds_status deserializeScore( ds_device* device, const unsigned char* serializedSc return DS_SUCCESS; } -ds_status releaseScore( void* score ) { +ds_status releaseScore(void *score) { delete (TessDeviceScore *)score; return DS_SUCCESS; } // evaluate devices ds_status evaluateScoreForDevice( ds_device *device, void *inputData) { - // overwrite statuc gpuEnv w/ current device // so native opencl calls can be used; they use static gpuEnv printf("\n[DS] Device: \"%s\" (%s) evaluation...\n", device->oclDeviceName, device->type==DS_DEVICE_OPENCL_DEVICE ? "OpenCL" : "Native" ); @@ -3233,14 +3058,12 @@ ds_status evaluateScoreForDevice( ds_device *device, void *inputData) { float composeRGBPixelWeight = 1.2f; float histogramRectWeight = 2.4f; float thresholdRectToPixWeight = 4.5f; - float getLineMasksMorphWeight = 5.0f; + float getLineMasksMorphWeight = 5.0f; - float weightedTime = - composeRGBPixelWeight * composeRGBPixelTime + - histogramRectWeight * histogramRectTime + - thresholdRectToPixWeight * thresholdRectToPixTime + - getLineMasksMorphWeight * getLineMasksMorphTime - ; + float weightedTime = composeRGBPixelWeight * composeRGBPixelTime + + histogramRectWeight * histogramRectTime + + thresholdRectToPixWeight * thresholdRectToPixTime + + getLineMasksMorphWeight * getLineMasksMorphTime; device->score = (void *)new TessDeviceScore; ((TessDeviceScore *)device->score)->time = weightedTime; @@ -3256,83 +3079,105 @@ ds_status evaluateScoreForDevice( ds_device *device, void *inputData) { // initial call to select device ds_device OpenclDevice::getDeviceSelection( ) { if (!deviceIsSelected) { -PERF_COUNT_START("getDeviceSelection") - // check if opencl is available at runtime - if( 1 == LoadOpencl() ) { - // opencl is available -//PERF_COUNT_SUB("LoadOpencl") - // setup devices - ds_status status; - ds_profile *profile; - status = initDSProfile( &profile, "v0.1" ); -PERF_COUNT_SUB("initDSProfile") - // try reading scores from file - const char *fileName = "tesseract_opencl_profile_devices.dat"; - status = readProfileFromFile( profile, deserializeScore, fileName); - if (status != DS_SUCCESS) { - // need to run evaluation - printf("[DS] Profile file not available (%s); performing profiling.\n", fileName); + PERF_COUNT_START("getDeviceSelection") + // check if opencl is available at runtime + if (1 == LoadOpencl()) { + // opencl is available + // PERF_COUNT_SUB("LoadOpencl") + // setup devices + ds_status status; + ds_profile *profile; + status = initDSProfile(&profile, "v0.1"); + PERF_COUNT_SUB("initDSProfile") + // try reading scores from file + const char *fileName = "tesseract_opencl_profile_devices.dat"; + status = readProfileFromFile(profile, deserializeScore, fileName); + if (status != DS_SUCCESS) { + // need to run evaluation + printf("[DS] Profile file not available (%s); performing profiling.\n", + fileName); - // create input data - TessScoreEvaluationInputData input; - populateTessScoreEvaluationInputData( &input ); -//PERF_COUNT_SUB("populateTessScoreEvaluationInputData") - // perform evaluations - unsigned int numUpdates; - status = profileDevices( profile, DS_EVALUATE_ALL, evaluateScoreForDevice, (void *)&input, &numUpdates ); -PERF_COUNT_SUB("profileDevices") - // write scores to file - if ( status == DS_SUCCESS ) { - status = writeProfileToFile( profile, serializeScore, fileName); -PERF_COUNT_SUB("writeProfileToFile") - if ( status == DS_SUCCESS ) { - printf("[DS] Scores written to file (%s).\n", fileName); + // create input data + TessScoreEvaluationInputData input; + populateTessScoreEvaluationInputData(&input); + // PERF_COUNT_SUB("populateTessScoreEvaluationInputData") + // perform evaluations + unsigned int numUpdates; + status = + profileDevices(profile, DS_EVALUATE_ALL, evaluateScoreForDevice, + (void *)&input, &numUpdates); + PERF_COUNT_SUB("profileDevices") + // write scores to file + if (status == DS_SUCCESS) { + status = writeProfileToFile(profile, serializeScore, fileName); + PERF_COUNT_SUB("writeProfileToFile") + if (status == DS_SUCCESS) { + printf("[DS] Scores written to file (%s).\n", fileName); + } else { + printf( + "[DS] Error saving scores to file (%s); scores not written to " + "file.\n", + fileName); + } } else { - printf("[DS] Error saving scores to file (%s); scores not written to file.\n", fileName); + printf( + "[DS] Unable to evaluate performance; scores not written to " + "file.\n"); } } else { - printf("[DS] Unable to evaluate performance; scores not written to file.\n"); + PERF_COUNT_SUB("readProfileFromFile") + printf("[DS] Profile read from file (%s).\n", fileName); } - } else { -PERF_COUNT_SUB("readProfileFromFile") - printf("[DS] Profile read from file (%s).\n", fileName); - } + // we now have device scores either from file or evaluation + // select fastest using custom Tesseract selection algorithm + float bestTime = FLT_MAX; // begin search with worst possible time + int bestDeviceIdx = -1; + for (int d = 0; d < profile->numDevices; d++) { + ds_device device = profile->devices[d]; + TessDeviceScore score = *(TessDeviceScore *)device.score; - // we now have device scores either from file or evaluation - // select fastest using custom Tesseract selection algorithm - float bestTime = FLT_MAX; // begin search with worst possible time - int bestDeviceIdx = -1; - for (int d = 0; d < profile->numDevices; d++) { - ds_device device = profile->devices[d]; - TessDeviceScore score = *(TessDeviceScore *)device.score; - - float time = score.time; - printf("[DS] Device[%i] %i:%s score is %f\n", d+1, device.type, device.oclDeviceName, time); - if (time < bestTime) { - bestTime = time; + float time = score.time; + printf("[DS] Device[%i] %i:%s score is %f\n", d + 1, device.type, + device.oclDeviceName, time); + if (time < bestTime) { + bestTime = time; bestDeviceIdx = d; + } } - } - printf("[DS] Selected Device[%i]: \"%s\" (%s)\n", bestDeviceIdx+1, profile->devices[bestDeviceIdx].oclDeviceName, profile->devices[bestDeviceIdx].type==DS_DEVICE_OPENCL_DEVICE ? "OpenCL" : "Native"); - // cleanup - // TODO: call destructor for profile object? + printf("[DS] Selected Device[%i]: \"%s\" (%s)\n", bestDeviceIdx + 1, + profile->devices[bestDeviceIdx].oclDeviceName, + profile->devices[bestDeviceIdx].type == DS_DEVICE_OPENCL_DEVICE + ? "OpenCL" + : "Native"); + // cleanup + // TODO: call destructor for profile object? bool overrided = false; char *overrideDeviceStr = getenv("TESSERACT_OPENCL_DEVICE"); if (overrideDeviceStr != NULL) { int overrideDeviceIdx = atoi(overrideDeviceStr); - if (overrideDeviceIdx > 0 && overrideDeviceIdx <= profile->numDevices ) { - printf("[DS] Overriding Device Selection (TESSERACT_OPENCL_DEVICE=%s, %i)\n", overrideDeviceStr, overrideDeviceIdx); + if (overrideDeviceIdx > 0 && overrideDeviceIdx <= profile->numDevices) { + printf( + "[DS] Overriding Device Selection (TESSERACT_OPENCL_DEVICE=%s, " + "%i)\n", + overrideDeviceStr, overrideDeviceIdx); bestDeviceIdx = overrideDeviceIdx - 1; overrided = true; } else { - printf("[DS] Ignoring invalid TESSERACT_OPENCL_DEVICE=%s ([1,%i] are valid devices).\n", overrideDeviceStr, profile->numDevices); + printf( + "[DS] Ignoring invalid TESSERACT_OPENCL_DEVICE=%s ([1,%i] are " + "valid devices).\n", + overrideDeviceStr, profile->numDevices); } } if (overrided) { - printf("[DS] Overridden Device[%i]: \"%s\" (%s)\n", bestDeviceIdx+1, profile->devices[bestDeviceIdx].oclDeviceName, profile->devices[bestDeviceIdx].type==DS_DEVICE_OPENCL_DEVICE ? "OpenCL" : "Native"); + printf("[DS] Overridden Device[%i]: \"%s\" (%s)\n", bestDeviceIdx + 1, + profile->devices[bestDeviceIdx].oclDeviceName, + profile->devices[bestDeviceIdx].type == DS_DEVICE_OPENCL_DEVICE + ? "OpenCL" + : "Native"); } selectedDevice = profile->devices[bestDeviceIdx]; // cleanup @@ -3347,10 +3192,10 @@ PERF_COUNT_SUB("readProfileFromFile") selectedDevice.oclDriverVersion = NULL; } deviceIsSelected = true; -PERF_COUNT_SUB("select from Profile") -PERF_COUNT_END + PERF_COUNT_SUB("select from Profile") + PERF_COUNT_END } -//PERF_COUNT_END + // PERF_COUNT_END return selectedDevice; } @@ -3365,8 +3210,6 @@ bool OpenclDevice::selectedDeviceIsNativeCPU() { return (device.type == DS_DEVICE_NATIVE_CPU); } - - /*! * pixConvertRGBToGray() from leptonica, converted to opencl kernel * @@ -3378,111 +3221,113 @@ bool OpenclDevice::selectedDeviceIsNativeCPU() { * Notes: * (1) Use a weighted average of the RGB values. */ -#define SET_DATA_BYTE( pdata, n, val ) (*(l_uint8 *)((l_uintptr_t)((l_uint8 *)(pdata) + (n)) ^ 3) = (val)) +#define SET_DATA_BYTE(pdata, n, val) \ + (*(l_uint8 *)((l_uintptr_t)((l_uint8 *)(pdata) + (n)) ^ 3) = (val)) -Pix * OpenclDevice::pixConvertRGBToGrayOCL( - Pix *srcPix, // 32-bit source - float rwt, - float gwt, - float bwt ) -{ -PERF_COUNT_START("pixConvertRGBToGrayOCL") - Pix *dstPix; // 8-bit destination +Pix *OpenclDevice::pixConvertRGBToGrayOCL(Pix *srcPix, // 32-bit source + float rwt, float gwt, float bwt) { + PERF_COUNT_START("pixConvertRGBToGrayOCL") + Pix *dstPix; // 8-bit destination - if (rwt < 0.0 || gwt < 0.0 || bwt < 0.0) return NULL; + if (rwt < 0.0 || gwt < 0.0 || bwt < 0.0) return NULL; - if (rwt == 0.0 && gwt == 0.0 && bwt == 0.0) { - // magic numbers from leptonica - rwt = 0.3; - gwt = 0.5; - bwt = 0.2; - } - // normalize - float sum = rwt + gwt + bwt; - rwt /= sum; - gwt /= sum; - bwt /= sum; + if (rwt == 0.0 && gwt == 0.0 && bwt == 0.0) { + // magic numbers from leptonica + rwt = 0.3; + gwt = 0.5; + bwt = 0.2; + } + // normalize + float sum = rwt + gwt + bwt; + rwt /= sum; + gwt /= sum; + bwt /= sum; - // source pix - int w, h; - pixGetDimensions(srcPix, &w, &h, NULL); - //printf("Image is %i x %i\n", w, h); - unsigned int *srcData = pixGetData(srcPix); - int srcWPL = pixGetWpl(srcPix); - int srcSize = srcWPL * h * sizeof(unsigned int); + // source pix + int w, h; + pixGetDimensions(srcPix, &w, &h, NULL); + // printf("Image is %i x %i\n", w, h); + unsigned int *srcData = pixGetData(srcPix); + int srcWPL = pixGetWpl(srcPix); + int srcSize = srcWPL * h * sizeof(unsigned int); - // destination pix - if ((dstPix = pixCreate(w, h, 8)) == NULL) - return NULL; - pixCopyResolution(dstPix, srcPix); - unsigned int *dstData = pixGetData(dstPix); - int dstWPL = pixGetWpl(dstPix); - int dstWords = dstWPL * h; - int dstSize = dstWords * sizeof(unsigned int); - //printf("dstSize = %i\n", dstSize); -PERF_COUNT_SUB("pix setup") + // destination pix + if ((dstPix = pixCreate(w, h, 8)) == NULL) return NULL; + pixCopyResolution(dstPix, srcPix); + unsigned int *dstData = pixGetData(dstPix); + int dstWPL = pixGetWpl(dstPix); + int dstWords = dstWPL * h; + int dstSize = dstWords * sizeof(unsigned int); + // printf("dstSize = %i\n", dstSize); + PERF_COUNT_SUB("pix setup") - // opencl objects - cl_int clStatus; - KernelEnv kEnv; - SetKernelEnv( &kEnv ); + // opencl objects + cl_int clStatus; + KernelEnv kEnv; + SetKernelEnv(&kEnv); - // source buffer - cl_mem srcBuffer = clCreateBuffer( kEnv.mpkContext, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, srcSize, (void *)srcData, &clStatus ); - CHECK_OPENCL( clStatus, "clCreateBuffer srcBuffer"); + // source buffer + cl_mem srcBuffer = + clCreateBuffer(kEnv.mpkContext, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, + srcSize, (void *)srcData, &clStatus); + CHECK_OPENCL(clStatus, "clCreateBuffer srcBuffer"); - // destination buffer - cl_mem dstBuffer = clCreateBuffer( kEnv.mpkContext, CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, dstSize, (void *)dstData, &clStatus ); - CHECK_OPENCL( clStatus, "clCreateBuffer dstBuffer"); + // destination buffer + cl_mem dstBuffer = + clCreateBuffer(kEnv.mpkContext, CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, + dstSize, (void *)dstData, &clStatus); + CHECK_OPENCL(clStatus, "clCreateBuffer dstBuffer"); - // setup work group size parameters - int block_size = 256; - int numWorkGroups = ((h*w+block_size-1) / block_size ); - int numThreads = block_size*numWorkGroups; - size_t local_work_size[] = {static_cast(block_size)}; - size_t global_work_size[] = {static_cast(numThreads)}; - //printf("Enqueueing %i threads for %i output pixels\n", numThreads, w*h); + // setup work group size parameters + int block_size = 256; + int numWorkGroups = ((h * w + block_size - 1) / block_size); + int numThreads = block_size * numWorkGroups; + size_t local_work_size[] = {static_cast(block_size)}; + size_t global_work_size[] = {static_cast(numThreads)}; + // printf("Enqueueing %i threads for %i output pixels\n", numThreads, w*h); - /* compile kernel */ - kEnv.mpkKernel = clCreateKernel( kEnv.mpkProgram, "kernel_RGBToGray", &clStatus ); - CHECK_OPENCL( clStatus, "clCreateKernel kernel_RGBToGray"); + /* compile kernel */ + kEnv.mpkKernel = + clCreateKernel(kEnv.mpkProgram, "kernel_RGBToGray", &clStatus); + CHECK_OPENCL(clStatus, "clCreateKernel kernel_RGBToGray"); + /* set kernel arguments */ + clStatus = + clSetKernelArg(kEnv.mpkKernel, 0, sizeof(cl_mem), (void *)&srcBuffer); + CHECK_OPENCL(clStatus, "clSetKernelArg srcBuffer"); + clStatus = + clSetKernelArg(kEnv.mpkKernel, 1, sizeof(cl_mem), (void *)&dstBuffer); + CHECK_OPENCL(clStatus, "clSetKernelArg dstBuffer"); + clStatus = clSetKernelArg(kEnv.mpkKernel, 2, sizeof(int), (void *)&srcWPL); + CHECK_OPENCL(clStatus, "clSetKernelArg srcWPL"); + clStatus = clSetKernelArg(kEnv.mpkKernel, 3, sizeof(int), (void *)&dstWPL); + CHECK_OPENCL(clStatus, "clSetKernelArg dstWPL"); + clStatus = clSetKernelArg(kEnv.mpkKernel, 4, sizeof(int), (void *)&h); + CHECK_OPENCL(clStatus, "clSetKernelArg height"); + clStatus = clSetKernelArg(kEnv.mpkKernel, 5, sizeof(int), (void *)&w); + CHECK_OPENCL(clStatus, "clSetKernelArg width"); + clStatus = clSetKernelArg(kEnv.mpkKernel, 6, sizeof(float), (void *)&rwt); + CHECK_OPENCL(clStatus, "clSetKernelArg rwt"); + clStatus = clSetKernelArg(kEnv.mpkKernel, 7, sizeof(float), (void *)&gwt); + CHECK_OPENCL(clStatus, "clSetKernelArg gwt"); + clStatus = clSetKernelArg(kEnv.mpkKernel, 8, sizeof(float), (void *)&bwt); + CHECK_OPENCL(clStatus, "clSetKernelArg bwt"); - /* set kernel arguments */ - clStatus = clSetKernelArg( kEnv.mpkKernel, 0, sizeof(cl_mem), (void *)&srcBuffer ); - CHECK_OPENCL( clStatus, "clSetKernelArg srcBuffer"); - clStatus = clSetKernelArg( kEnv.mpkKernel, 1, sizeof(cl_mem), (void *)&dstBuffer ); - CHECK_OPENCL( clStatus, "clSetKernelArg dstBuffer"); - clStatus = clSetKernelArg( kEnv.mpkKernel, 2, sizeof(int), (void *)&srcWPL ); - CHECK_OPENCL( clStatus, "clSetKernelArg srcWPL" ); - clStatus = clSetKernelArg( kEnv.mpkKernel, 3, sizeof(int), (void *)&dstWPL ); - CHECK_OPENCL( clStatus, "clSetKernelArg dstWPL" ); - clStatus = clSetKernelArg( kEnv.mpkKernel, 4, sizeof(int), (void *)&h ); - CHECK_OPENCL( clStatus, "clSetKernelArg height" ); - clStatus = clSetKernelArg( kEnv.mpkKernel, 5, sizeof(int), (void *)&w ); - CHECK_OPENCL( clStatus, "clSetKernelArg width" ); - clStatus = clSetKernelArg( kEnv.mpkKernel, 6, sizeof(float), (void *)&rwt ); - CHECK_OPENCL( clStatus, "clSetKernelArg rwt" ); - clStatus = clSetKernelArg( kEnv.mpkKernel, 7, sizeof(float), (void *)&gwt ); - CHECK_OPENCL( clStatus, "clSetKernelArg gwt"); - clStatus = clSetKernelArg( kEnv.mpkKernel, 8, sizeof(float), (void *)&bwt ); - CHECK_OPENCL( clStatus, "clSetKernelArg bwt"); + /* launch kernel & wait */ + PERF_COUNT_SUB("before") + clStatus = + clEnqueueNDRangeKernel(kEnv.mpkCmdQueue, kEnv.mpkKernel, 1, NULL, + global_work_size, local_work_size, 0, NULL, NULL); + CHECK_OPENCL(clStatus, "clEnqueueNDRangeKernel kernel_RGBToGray"); + clFinish(kEnv.mpkCmdQueue); + PERF_COUNT_SUB("kernel") - /* launch kernel & wait */ -PERF_COUNT_SUB("before") - clStatus = clEnqueueNDRangeKernel( - kEnv.mpkCmdQueue, - kEnv.mpkKernel, - 1, NULL, global_work_size, local_work_size, - 0, NULL, NULL ); - CHECK_OPENCL( clStatus, "clEnqueueNDRangeKernel kernel_RGBToGray" ); - clFinish( kEnv.mpkCmdQueue ); -PERF_COUNT_SUB("kernel") - - /* map results back from gpu */ - void *ptr = clEnqueueMapBuffer(kEnv.mpkCmdQueue, dstBuffer, CL_TRUE, CL_MAP_READ, 0, dstSize, 0, NULL, NULL, &clStatus); - CHECK_OPENCL( clStatus, "clEnqueueMapBuffer dstBuffer"); - clEnqueueUnmapMemObject(rEnv.mpkCmdQueue, dstBuffer, ptr, 0, NULL, NULL); + /* map results back from gpu */ + void *ptr = + clEnqueueMapBuffer(kEnv.mpkCmdQueue, dstBuffer, CL_TRUE, CL_MAP_READ, 0, + dstSize, 0, NULL, NULL, &clStatus); + CHECK_OPENCL(clStatus, "clEnqueueMapBuffer dstBuffer"); + clEnqueueUnmapMemObject(rEnv.mpkCmdQueue, dstBuffer, ptr, 0, NULL, NULL); #if 0 // validate: compute on cpu @@ -3519,13 +3364,12 @@ PERF_COUNT_SUB("kernel") //printf("\n"); } #endif - // release opencl objects - clReleaseMemObject(srcBuffer); - clReleaseMemObject(dstBuffer); + // release opencl objects + clReleaseMemObject(srcBuffer); + clReleaseMemObject(dstBuffer); - -PERF_COUNT_END - // success - return dstPix; + PERF_COUNT_END + // success + return dstPix; } #endif diff --git a/opencl/openclwrapper.h b/opencl/openclwrapper.h index ae52a8015..5aa625bc4 100644 --- a/opencl/openclwrapper.h +++ b/opencl/openclwrapper.h @@ -10,7 +10,8 @@ // including CL/cl.h doesn't occur until USE_OPENCL defined below // platform preprocessor commands -#if defined( WIN32 ) || defined( __WIN32__ ) || defined( _WIN32 ) || defined( __CYGWIN__ ) || defined( __MINGW32__ ) +#if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || \ + defined(__CYGWIN__) || defined(__MINGW32__) #define ON_WINDOWS 1 #define ON_LINUX 0 #define ON_APPLE 0 @@ -80,21 +81,23 @@ time_sub_start = time_funct_start; \ time_sub_end = time_funct_start; -#define PERF_COUNT_END \ - QueryPerformanceCounter(&time_funct_end); \ - elapsed_time_sec = (time_funct_end.QuadPart-time_funct_start.QuadPart)/(double)(freq.QuadPart); \ - printf(PERF_COUNT_REPORT_STR, funct_name, "total", elapsed_time_sec); +#define PERF_COUNT_END \ + QueryPerformanceCounter(&time_funct_end); \ + elapsed_time_sec = (time_funct_end.QuadPart - time_funct_start.QuadPart) / \ + (double)(freq.QuadPart); \ + printf(PERF_COUNT_REPORT_STR, funct_name, "total", elapsed_time_sec); #else #define PERF_COUNT_START(FUNCT_NAME) #define PERF_COUNT_END #endif #if PERF_COUNT_VERBOSE >= 3 -#define PERF_COUNT_SUB(SUB) \ - QueryPerformanceCounter(&time_sub_end); \ - elapsed_time_sec = (time_sub_end.QuadPart-time_sub_start.QuadPart)/(double)(freq.QuadPart); \ - printf(PERF_COUNT_REPORT_STR, funct_name, SUB, elapsed_time_sec); \ - time_sub_start = time_sub_end; +#define PERF_COUNT_SUB(SUB) \ + QueryPerformanceCounter(&time_sub_end); \ + elapsed_time_sec = (time_sub_end.QuadPart - time_sub_start.QuadPart) / \ + (double)(freq.QuadPart); \ + printf(PERF_COUNT_REPORT_STR, funct_name, SUB, elapsed_time_sec); \ + time_sub_start = time_sub_end; #else #define PERF_COUNT_SUB(SUB) #endif @@ -112,21 +115,25 @@ time_sub_start = time_funct_start; \ time_sub_end = time_funct_start; -#define PERF_COUNT_END \ - clock_gettime( CLOCK_MONOTONIC, &time_funct_end ); \ - elapsed_time_sec = (time_funct_end.tv_sec - time_funct_start.tv_sec)*1.0 + (time_funct_end.tv_nsec - time_funct_start.tv_nsec)/1000000000.0; \ - printf(PERF_COUNT_REPORT_STR, funct_name, "total", elapsed_time_sec); +#define PERF_COUNT_END \ + clock_gettime(CLOCK_MONOTONIC, &time_funct_end); \ + elapsed_time_sec = \ + (time_funct_end.tv_sec - time_funct_start.tv_sec) * 1.0 + \ + (time_funct_end.tv_nsec - time_funct_start.tv_nsec) / 1000000000.0; \ + printf(PERF_COUNT_REPORT_STR, funct_name, "total", elapsed_time_sec); #else #define PERF_COUNT_START(FUNCT_NAME) #define PERF_COUNT_END #endif #if PERF_COUNT_VERBOSE >= 3 -#define PERF_COUNT_SUB(SUB) \ - clock_gettime( CLOCK_MONOTONIC, &time_sub_end ); \ - elapsed_time_sec = (time_sub_end.tv_sec - time_sub_start.tv_sec)*1.0 + (time_sub_end.tv_nsec - time_sub_start.tv_nsec)/1000000000.0; \ - printf(PERF_COUNT_REPORT_STR, funct_name, SUB, elapsed_time_sec); \ - time_sub_start = time_sub_end; +#define PERF_COUNT_SUB(SUB) \ + clock_gettime(CLOCK_MONOTONIC, &time_sub_end); \ + elapsed_time_sec = \ + (time_sub_end.tv_sec - time_sub_start.tv_sec) * 1.0 + \ + (time_sub_end.tv_nsec - time_sub_start.tv_nsec) / 1000000000.0; \ + printf(PERF_COUNT_REPORT_STR, funct_name, SUB, elapsed_time_sec); \ + time_sub_start = time_sub_end; #else #define PERF_COUNT_SUB(SUB) #endif @@ -253,12 +260,12 @@ public: // OpenCL implementation of Morphology (Hollow = Closed - Open) static PIX* pixHollowCL(PIX *pixd, PIX *pixs, l_int32 close_hsize, l_int32 close_vsize, l_int32 open_hsize, l_int32 open_vsize, bool reqDataCopy); - static void pixGetLinesCL(PIX *pixd, PIX *pixs, - PIX** pix_vline, PIX** pix_hline, - PIX** pixClosed, bool getpixClosed, - l_int32 close_hsize, l_int32 close_vsize, - l_int32 open_hsize, l_int32 open_vsize, - l_int32 line_hsize, l_int32 line_vsize); + static void pixGetLinesCL(PIX *pixd, PIX *pixs, PIX **pix_vline, + PIX **pix_hline, PIX **pixClosed, + bool getpixClosed, l_int32 close_hsize, + l_int32 close_vsize, l_int32 open_hsize, + l_int32 open_vsize, l_int32 line_hsize, + l_int32 line_vsize); //int InitOpenclAttr( OpenCLEnv * env ); //int ReleaseKernel( KernelEnv * env ); @@ -279,34 +286,24 @@ public: static void FreeOpenclDll(); #endif - inline static int AddKernelConfig( int kCount, const char *kName ); /* for binarization */ - static int HistogramRectOCL( - const unsigned char *imagedata, - int bytes_per_pixel, - int bytes_per_line, - int left, - int top, - int width, - int height, - int kHistogramSize, - int *histogramAllChannels); + static int HistogramRectOCL(const unsigned char *imagedata, + int bytes_per_pixel, int bytes_per_line, + int left, int top, int width, int height, + int kHistogramSize, int *histogramAllChannels); - static int ThresholdRectToPixOCL( - const unsigned char* imagedata, - int bytes_per_pixel, - int bytes_per_line, - const int* thresholds, - const int* hi_values, - Pix** pix, - int rect_height, - int rect_width, - int rect_top, - int rect_left); + static int ThresholdRectToPixOCL(const unsigned char *imagedata, + int bytes_per_pixel, int bytes_per_line, + const int *thresholds, + const int *hi_values, Pix **pix, + int rect_height, int rect_width, + int rect_top, int rect_left); - static Pix * pixConvertRGBToGrayOCL( Pix *pix, float weightRed = 0.3, float weightGreen = 0.5, float weightBlue = 0.2 ); + static Pix *pixConvertRGBToGrayOCL(Pix *pix, float weightRed = 0.3, + float weightGreen = 0.5, + float weightBlue = 0.2); static ds_device getDeviceSelection(); static ds_device selectedDevice; diff --git a/tessdata/configs/box.train.stderr b/tessdata/configs/box.train.stderr index 6fc51fdd5..d44ff2b2c 100644 --- a/tessdata/configs/box.train.stderr +++ b/tessdata/configs/box.train.stderr @@ -1,7 +1,7 @@ -file_type .bl -#tessedit_use_nn F -textord_fast_pitch_test T -tessedit_single_match 0 +file_type .bl +#tessedit_use_nn F +textord_fast_pitch_test T +tessedit_single_match 0 tessedit_zero_rejection T tessedit_minimal_rejection F tessedit_write_rep_codes F diff --git a/textord/alignedblob.cpp b/textord/alignedblob.cpp index 007d4ad38..0dab26bf8 100644 --- a/textord/alignedblob.cpp +++ b/textord/alignedblob.cpp @@ -188,7 +188,7 @@ ScrollView* AlignedBlob::DisplayTabs(const char* window_name, gsearch.StartFullSearch(); BLOBNBOX* bbox; while ((bbox = gsearch.NextFullSearch()) != NULL) { - TBOX box = bbox->bounding_box(); + const TBOX& box = bbox->bounding_box(); int left_x = box.left(); int right_x = box.right(); int top_y = box.top(); diff --git a/textord/bbgrid.cpp b/textord/bbgrid.cpp index 06114748f..4cadcdcf2 100644 --- a/textord/bbgrid.cpp +++ b/textord/bbgrid.cpp @@ -231,7 +231,7 @@ Pix* GridReducedPix(const TBOX& box, int gridsize, // Note that the Pix is used upside-down, with (0, 0) being the bottom-left. Pix* TraceOutlineOnReducedPix(C_OUTLINE* outline, int gridsize, ICOORD bleft, int* left, int* bottom) { - TBOX box = outline->bounding_box(); + const TBOX& box = outline->bounding_box(); Pix* pix = GridReducedPix(box, gridsize, bleft, left, bottom); int wpl = pixGetWpl(pix); l_uint32* data = pixGetData(pix); @@ -257,7 +257,7 @@ Pix* TraceOutlineOnReducedPix(C_OUTLINE* outline, int gridsize, // As TraceOutlineOnReducedPix above, but on a BLOCK instead of a C_OUTLINE. Pix* TraceBlockOnReducedPix(BLOCK* block, int gridsize, ICOORD bleft, int* left, int* bottom) { - TBOX box = block->bounding_box(); + const TBOX& box = block->bounding_box(); Pix* pix = GridReducedPix(box, gridsize, bleft, left, bottom); int wpl = pixGetWpl(pix); l_uint32* data = pixGetData(pix); diff --git a/textord/bbgrid.h b/textord/bbgrid.h index d16b902ec..cde4b93a6 100644 --- a/textord/bbgrid.h +++ b/textord/bbgrid.h @@ -623,7 +623,7 @@ void BBGrid::DisplayBoxes(ScrollView* tab_win) { gsearch.StartFullSearch(); BBC* bbox; while ((bbox = gsearch.NextFullSearch()) != NULL) { - TBOX box = bbox->bounding_box(); + const TBOX& box = bbox->bounding_box(); int left_x = box.left(); int right_x = box.right(); int top_y = box.top(); diff --git a/textord/blkocc.h b/textord/blkocc.h index 89462dc86..8305c36cd 100644 --- a/textord/blkocc.h +++ b/textord/blkocc.h @@ -72,12 +72,12 @@ ELISTIZEH (REGION_OCC) Adapted from the following procedure so that it can be used in the bands class in an include file... -BOOL8 range_in_band[ +BOOL8 range_in_band[ range within band? -inT16 band_max, -inT16 band_min, -inT16 range_max, -inT16 range_min] +inT16 band_max, +inT16 band_min, +inT16 range_max, +inT16 range_min] { if ( (range_min >= band_min) && (range_max < band_max) ) return TRUE; @@ -91,12 +91,12 @@ inT16 range_min] Adapted from the following procedure so that it can be used in the bands class in an include file... -BOOL8 range_overlaps_band[ +BOOL8 range_overlaps_band[ range crosses band? -inT16 band_max, -inT16 band_min, -inT16 range_max, -inT16 range_min] +inT16 band_max, +inT16 band_min, +inT16 range_max, +inT16 range_min] { if ( (range_max >= band_min) && (range_min < band_max) ) return TRUE; diff --git a/textord/ccnontextdetect.cpp b/textord/ccnontextdetect.cpp index 1cb0e4c6c..f6a7d8f41 100644 --- a/textord/ccnontextdetect.cpp +++ b/textord/ccnontextdetect.cpp @@ -305,7 +305,7 @@ bool CCNonTextDetect::BlobOverlapsTooMuch(BLOBNBOX* blob, int max_overlaps) { // Search the grid to see what intersects it. // Setup a Rectangle search for overlapping this blob. BlobGridSearch rsearch(this); - TBOX box = blob->bounding_box(); + const TBOX& box = blob->bounding_box(); rsearch.StartRectSearch(box); rsearch.SetUniqueMode(true); BLOBNBOX* neighbour; diff --git a/textord/colpartition.cpp b/textord/colpartition.cpp index effb5a911..e8e8502e8 100644 --- a/textord/colpartition.cpp +++ b/textord/colpartition.cpp @@ -918,7 +918,7 @@ void ColPartition::ComputeLimits() { for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { bbox = it.data(); if (non_leader_count == 0 || bbox->flow() != BTFT_LEADER) { - TBOX box = bbox->bounding_box(); + const TBOX& box = bbox->bounding_box(); int area = box.area(); top_stats.add(box.top(), area); bottom_stats.add(box.bottom(), area); @@ -1181,8 +1181,8 @@ bool ColPartition::MarkAsLeaderIfMonospaced() { if (best_end == NULL) { tprintf("No path\n"); } else { - tprintf("Total cost = %d vs allowed %d\n", - best_end->total_cost(), blob_count); + tprintf("Total cost = %d vs allowed %d\n", best_end->total_cost(), + blob_count); } } delete [] projection; @@ -2121,7 +2121,7 @@ void ColPartition::RefinePartnersByOverlap(bool upper, // Return true if bbox belongs better in this than other. bool ColPartition::ThisPartitionBetter(BLOBNBOX* bbox, const ColPartition& other) { - TBOX box = bbox->bounding_box(); + const TBOX& box = bbox->bounding_box(); // Margins take priority. int left = box.left(); int right = box.right(); diff --git a/textord/colpartitiongrid.cpp b/textord/colpartitiongrid.cpp index 4d703fbe7..c87340862 100644 --- a/textord/colpartitiongrid.cpp +++ b/textord/colpartitiongrid.cpp @@ -86,7 +86,7 @@ void ColPartitionGrid::HandleClick(int x, int y) { ColPartition* neighbour; FCOORD click(x, y); while ((neighbour = radsearch.NextRadSearch()) != NULL) { - TBOX nbox = neighbour->bounding_box(); + const TBOX& nbox = neighbour->bounding_box(); if (nbox.contains(click)) { tprintf("Block box:"); neighbour->bounding_box().print(); @@ -1037,7 +1037,7 @@ void ColPartitionGrid::ListFindMargins(ColPartitionSet** best_columns, ColPartition* part = part_it.data(); ColPartitionSet* columns = NULL; if (best_columns != NULL) { - TBOX part_box = part->bounding_box(); + const TBOX& part_box = part->bounding_box(); // Get the columns from the y grid coord. int grid_x, grid_y; GridCoords(part_box.left(), part_box.bottom(), &grid_x, &grid_y); @@ -1569,7 +1569,7 @@ BlobRegionType ColPartitionGrid::SmoothInOneDirection( const TBOX& im_box, const FCOORD& rerotation, bool debug, const ColPartition& part, int* best_distance) { // Set up a rectangle search bounded by the part. - TBOX part_box = part.bounding_box(); + const TBOX& part_box = part.bounding_box(); TBOX search_box; ICOORD dist_scaling; ComputeSearchBoxAndScaling(direction, part_box, gridsize(), @@ -1619,10 +1619,10 @@ BlobRegionType ColPartitionGrid::SmoothInOneDirection( image_bias - htext_score >= kSmoothDecisionMargin && image_bias - vtext_score >= kSmoothDecisionMargin) { *best_distance = dists[NPT_IMAGE][0]; - if (dists[NPT_WEAK_VTEXT].size() > 0 && + if (!dists[NPT_WEAK_VTEXT].empty() && *best_distance > dists[NPT_WEAK_VTEXT][0]) *best_distance = dists[NPT_WEAK_VTEXT][0]; - if (dists[NPT_WEAK_HTEXT].size() > 0 && + if (!dists[NPT_WEAK_HTEXT].empty() && *best_distance > dists[NPT_WEAK_HTEXT][0]) *best_distance = dists[NPT_WEAK_HTEXT][0]; return BRT_POLYIMAGE; diff --git a/textord/devanagari_processing.h b/textord/devanagari_processing.h index 990a5dfe3..ef3acf745 100644 --- a/textord/devanagari_processing.h +++ b/textord/devanagari_processing.h @@ -41,9 +41,7 @@ class PixelHistogram { length_ = 0; } - int* hist() const { - return hist_; - } + int* hist() const { return hist_; } int length() const { return length_; diff --git a/textord/drawedg.h b/textord/drawedg.h index 6bf062d4e..ef5ed5e20 100644 --- a/textord/drawedg.h +++ b/textord/drawedg.h @@ -1,8 +1,9 @@ /********************************************************************** * File: drawedg.h (Formerly drawedge.h) - * Description: Collection of functions to draw things to do with edge detection. - * Author: Ray Smith - * Created: Thu Jun 06 13:29:20 BST 1991 + * Description: Collection of functions to draw things to do with edge + *detection. + * Author: Ray Smith + * Created: Thu Jun 06 13:29:20 BST 1991 * * (C) Copyright 1991, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/textord/fpchop.cpp b/textord/fpchop.cpp index 4c18338b8..be2768cce 100644 --- a/textord/fpchop.cpp +++ b/textord/fpchop.cpp @@ -1,8 +1,8 @@ /********************************************************************** * File: fpchop.cpp (Formerly fp_chop.c) * Description: Code to chop fixed pitch text into character cells. - * Author: Ray Smith - * Created: Thu Sep 16 11:14:15 BST 1993 + * Author: Ray Smith + * Created: Thu Sep 16 11:14:15 BST 1993 * * (C) Copyright 1993, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -730,7 +730,6 @@ C_OUTLINE *join_chopped_fragments( //join pieces return NULL; } - /********************************************************************** * join_segments * diff --git a/textord/imagefind.cpp b/textord/imagefind.cpp index c119e69f9..14442d52e 100644 --- a/textord/imagefind.cpp +++ b/textord/imagefind.cpp @@ -77,7 +77,7 @@ Pix* ImageFind::FindImages(Pix* pix) { // Leptonica will print an error message and return NULL if we call // pixGenHalftoneMask(pixr, NULL, ...) with too small image, so we // want to bypass that. - if (pixGetWidth(pixr) < kMinImageFindSize || + if (pixGetWidth(pixr) < kMinImageFindSize || pixGetHeight(pixr) < kMinImageFindSize) { pixDestroy(&pixr); return pixCreate(pixGetWidth(pix), pixGetHeight(pix), 1); @@ -1115,7 +1115,7 @@ static bool TestWeakIntersectedPart(const TBOX& im_box, ColPartition* part) { if (part->flow() < BTFT_STRONG_CHAIN) { // A weak partition intersects the box. - TBOX part_box = part->bounding_box(); + const TBOX& part_box = part->bounding_box(); if (im_box.contains(part_box)) { int area = part_box.area(); int intersect_area = IntersectArea(part_box, part_list); @@ -1180,7 +1180,7 @@ static bool ScanForOverlappingText(ColPartitionGrid* part_grid, TBOX* box) { part->flow() == BTFT_STRONG_CHAIN) { // Text intersects the box. any_text_in_padded_rect = true; - TBOX part_box = part->bounding_box(); + const TBOX& part_box = part->bounding_box(); if (box->overlap(part_box)) { return true; } diff --git a/textord/makerow.cpp b/textord/makerow.cpp index 1df4855b4..c8170e5d1 100644 --- a/textord/makerow.cpp +++ b/textord/makerow.cpp @@ -1,8 +1,8 @@ /********************************************************************** * File: makerow.cpp (Formerly makerows.c) * Description: Code to arrange blobs into rows of text. - * Author: Ray Smith - * Created: Mon Sep 21 14:34:48 BST 1992 + * Author: Ray Smith + * Created: Mon Sep 21 14:34:48 BST 1992 * * (C) Copyright 1992, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -671,7 +671,7 @@ BOOL8 find_best_dropout_row( //find neighbours TO_ROW_IT *row_it, //current position BOOL8 testing_on //correct orientation ) { - inT32 next_index; //of neighbouring row + inT32 next_index; // of neighbouring row inT32 row_offset; //from current row inT32 abs_dist; //absolute distance inT8 row_inc; //increment to row_index @@ -1786,7 +1786,7 @@ static int CountOverlaps(const TBOX& box, int min_height, BLOBNBOX_IT blob_it(blobs); for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) { BLOBNBOX* blob = blob_it.data(); - TBOX blob_box = blob->bounding_box(); + const TBOX &blob_box = blob->bounding_box(); if (blob_box.height() >= min_height && box.major_overlap(blob_box)) { ++overlaps; } diff --git a/textord/oldbasel.cpp b/textord/oldbasel.cpp index c73fe8d57..99e55fdbb 100644 --- a/textord/oldbasel.cpp +++ b/textord/oldbasel.cpp @@ -1,8 +1,8 @@ /********************************************************************** * File: oldbasel.cpp (Formerly oldbl.c) * Description: A re-implementation of the old baseline algorithm. - * Author: Ray Smith - * Created: Wed Oct 6 09:41:48 BST 1993 + * Author: Ray Smith + * Created: Wed Oct 6 09:41:48 BST 1993 * * (C) Copyright 1993, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -122,7 +122,7 @@ void Textord::correlate_lines(TO_BLOCK *block, float gradient) { TO_ROW **rows; //array of ptrs int rowcount; /*no of rows to do */ int rowindex; /*no of row */ - //iterator + // iterator TO_ROW_IT row_it = block->get_rows (); rowcount = row_it.length (); @@ -1018,61 +1018,6 @@ int *partcount /*no of partitions */ return bestpart; } - -///*merge_partitions(partids,partcount,blobcount,bestpart) discards funny looking -//partitions and gives all the rest partid 0*/ -// -//merge_partitions(partids,partcount,blobcount,bestpart) -//register char *partids; /*partition numbers*/ -//int partcount; /*no of partitions*/ -//int blobcount; /*no of blobs*/ -//int bestpart; /*best partition*/ -//{ -// int blobindex; /*no along text line*/ -// int runlength; /*run of same partition*/ -// int bestrun; /*biggest runlength*/ -// -// bestrun=0; /*no runs yet*/ -// runlength=1; -// for (blobindex=1;blobindexbestrun) -// bestrun=runlength; /*find biggest run*/ -// runlength=1; /*new run*/ -// } -// else -// { runlength++; -// } -// } -// if (runlength>bestrun) -// bestrun=runlength; -// -// for (blobindex=0;blobindex=blobcount -// || partids[blobindex]!=partids[blobindex+1]) -// /*loner*/ -// && (bestrun>2 || partids[blobindex]!=bestpart)) -// { partids[blobindex]=partcount; /*discard loner*/ -// } -// else if (blobindex+1=blobcount -// || partids[blobindex]!=partids[blobindex+2]) -// && (bestrun>3 || partids[blobindex]!=bestpart)) -// { partids[blobindex]=partcount; /*discard both*/ -// partids[blobindex+1]=partcount; -// } -// } -// } -// for (blobindex=0;blobindex= MINASCRISE && partsizes[partition] > poscount) { @@ -1459,8 +1402,8 @@ int blobcount, /*blobs in blobcoords */ QSPLINE * baseline, /*established */ float jumplimit /*min ascender height */ ) { - int blobindex; /*current blob */ - /*height statistics */ + int blobindex; /*current blob */ + /*height statistics */ STATS heightstat (0, MAXHEIGHT); int height; /*height of blob */ int xcentre; /*centre of blob */ diff --git a/textord/pithsync.h b/textord/pithsync.h index f9ba479e2..386426be7 100644 --- a/textord/pithsync.h +++ b/textord/pithsync.h @@ -1,8 +1,8 @@ /********************************************************************** * File: pithsync.h (Formerly pitsync2.h) * Description: Code to find the optimum fixed pitch segmentation of some blobs. - * Author: Ray Smith - * Created: Thu Nov 19 11:48:05 GMT 1992 + * Author: Ray Smith + * Created: Thu Nov 19 11:48:05 GMT 1992 * * (C) Copyright 1992, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -66,7 +66,7 @@ class FPCUTPT inT16 pitch, //proposed pitch inT16 pitch_error); //allowed tolerance - inT32 position() { //access func + inT32 position() { // access func return xpos; } double cost_function() { diff --git a/textord/pitsync1.h b/textord/pitsync1.h index c2fb9bec6..5374b003d 100644 --- a/textord/pitsync1.h +++ b/textord/pitsync1.h @@ -1,8 +1,8 @@ /********************************************************************** * File: pitsync1.h (Formerly pitsync.h) * Description: Code to find the optimum fixed pitch segmentation of some blobs. - * Author: Ray Smith - * Created: Thu Nov 19 11:48:05 GMT 1992 + * Author: Ray Smith + * Created: Thu Nov 19 11:48:05 GMT 1992 * * (C) Copyright 1992, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -46,7 +46,7 @@ class FPSEGPT:public ELIST_LINK FPSEGPT_LIST *prev_list); //previous segment FPSEGPT(FPCUTPT *cutpt); //build from new type - inT32 position() { //access func + inT32 position() { // access func return xpos; } double cost_function() { diff --git a/textord/strokewidth.cpp b/textord/strokewidth.cpp index 5d0fdc513..059aa9b85 100644 --- a/textord/strokewidth.cpp +++ b/textord/strokewidth.cpp @@ -393,7 +393,7 @@ void StrokeWidth::GradeBlobsIntoPartitions( } static void PrintBoxWidths(BLOBNBOX* neighbour) { - TBOX nbox = neighbour->bounding_box(); + const TBOX& nbox = neighbour->bounding_box(); tprintf("Box (%d,%d)->(%d,%d): h-width=%.1f, v-width=%.1f p-width=%1.f\n", nbox.left(), nbox.bottom(), nbox.right(), nbox.top(), neighbour->horz_stroke_width(), neighbour->vert_stroke_width(), @@ -1939,7 +1939,7 @@ ScrollView* StrokeWidth::DisplayGoodBlobs(const char* window_name, gsearch.StartFullSearch(); BLOBNBOX* bbox; while ((bbox = gsearch.NextFullSearch()) != NULL) { - TBOX box = bbox->bounding_box(); + const TBOX& box = bbox->bounding_box(); int left_x = box.left(); int right_x = box.right(); int top_y = box.top(); diff --git a/textord/tabfind.cpp b/textord/tabfind.cpp index dc7a072b7..30bad8bb3 100644 --- a/textord/tabfind.cpp +++ b/textord/tabfind.cpp @@ -229,7 +229,7 @@ void TabFind::GutterWidthAndNeighbourGap(int tab_x, int mean_height, bbox->flow() == BTFT_TEXT_ON_IMAGE, 0.0, *gutter_width, box.top(), box.bottom()); if (gutter_bbox != NULL) { - TBOX gutter_box = gutter_bbox->bounding_box(); + const TBOX& gutter_box = gutter_bbox->bounding_box(); *gutter_width = left ? tab_x - gutter_box.right() : gutter_box.left() - tab_x; } @@ -261,7 +261,7 @@ void TabFind::GutterWidthAndNeighbourGap(int tab_x, int mean_height, int neighbour_edge = left ? RightEdgeForBox(box, true, false) : LeftEdgeForBox(box, true, false); if (neighbour != NULL) { - TBOX n_box = neighbour->bounding_box(); + const TBOX& n_box = neighbour->bounding_box(); if (debug) { tprintf("Found neighbour:"); n_box.print(); diff --git a/textord/tablefind.cpp b/textord/tablefind.cpp index 425bdbc21..b68655a57 100644 --- a/textord/tablefind.cpp +++ b/textord/tablefind.cpp @@ -550,7 +550,7 @@ void TableFinder::GroupColumnBlocks(ColSegment_LIST* new_blocks, // iterate through the source list for (src_it.mark_cycle_pt(); !src_it.cycled_list(); src_it.forward()) { ColSegment* src_seg = src_it.data(); - TBOX src_box = src_seg->bounding_box(); + const TBOX& src_box = src_seg->bounding_box(); bool match_found = false; // iterate through the destination list to find a matching column block for (dest_it.mark_cycle_pt(); !dest_it.cycled_list(); dest_it.forward()) { @@ -1342,7 +1342,7 @@ void TableFinder::GetTableRegions(ColSegment_LIST* table_columns, // create a bool array to hold projection on y-axis bool* table_region = new bool[page_height]; while ((part = gsearch.NextFullSearch()) != NULL) { - TBOX part_box = part->bounding_box(); + const TBOX& part_box = part->bounding_box(); // reset the projection array for (int i = 0; i < page_height; i++) { table_region[i] = false; @@ -1974,7 +1974,7 @@ void TableFinder::DisplayColPartitionConnections( ColPartition* upper_part = part->nearest_neighbor_above(); if (upper_part) { - TBOX upper_box = upper_part->bounding_box(); + const TBOX& upper_box = upper_part->bounding_box(); int mid_x = (left_x + right_x) / 2; int mid_y = (top_y + bottom_y) / 2; int other_x = (upper_box.left() + upper_box.right()) / 2; @@ -1985,7 +1985,7 @@ void TableFinder::DisplayColPartitionConnections( } ColPartition* lower_part = part->nearest_neighbor_below(); if (lower_part) { - TBOX lower_box = lower_part->bounding_box(); + const TBOX& lower_box = lower_part->bounding_box(); int mid_x = (left_x + right_x) / 2; int mid_y = (top_y + bottom_y) / 2; int other_x = (lower_box.left() + lower_box.right()) / 2; @@ -2098,7 +2098,7 @@ void TableFinder::MakeTableBlocks(ColPartitionGrid* grid, table_search.StartFullSearch(); ColSegment* table; while ((table = table_search.NextFullSearch()) != NULL) { - TBOX table_box = table->bounding_box(); + const TBOX& table_box = table->bounding_box(); // Start a rect search on table_box GridSearch rectsearch(grid); diff --git a/textord/tabvector.cpp b/textord/tabvector.cpp index c8d508f0d..f3e99fa38 100644 --- a/textord/tabvector.cpp +++ b/textord/tabvector.cpp @@ -435,7 +435,7 @@ bool TabVector::SimilarTo(const ICOORD& vertical, vsearch.StartVerticalSearch(left, right, top_y); BLOBNBOX* blob; while ((blob = vsearch.NextVerticalSearch(true)) != NULL) { - TBOX box = blob->bounding_box(); + const TBOX& box = blob->bounding_box(); if (box.top() > bottom_y) return true; // Nothing found. if (box.bottom() < top_y) @@ -523,12 +523,12 @@ const char* kAlignmentNames[] = { // Print basic information about this tab vector. void TabVector::Print(const char* prefix) { - tprintf("%s %s (%d,%d)->(%d,%d) w=%d s=%d, sort key=%d, boxes=%d," - " partners=%d\n", - prefix, kAlignmentNames[alignment_], - startpt_.x(), startpt_.y(), endpt_.x(), endpt_.y(), - mean_width_, percent_score_, sort_key_, - boxes_.length(), partners_.length()); + tprintf( + "%s %s (%d,%d)->(%d,%d) w=%d s=%d, sort key=%d, boxes=%d," + " partners=%d\n", + prefix, kAlignmentNames[alignment_], startpt_.x(), startpt_.y(), + endpt_.x(), endpt_.y(), mean_width_, percent_score_, sort_key_, + boxes_.length(), partners_.length()); } // Print basic information about this tab vector and every box in it. @@ -806,7 +806,7 @@ bool TabVector::Fit(ICOORD vertical, bool force_parallel) { // Fit a line to all the boxes in the list. for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { BLOBNBOX* bbox = it.data(); - TBOX box = bbox->bounding_box(); + const TBOX& box = bbox->bounding_box(); int x1 = IsRightTab() ? box.right() : box.left(); ICOORD boxpt(x1, box.bottom()); linepoints.Add(boxpt); @@ -831,7 +831,7 @@ bool TabVector::Fit(ICOORD vertical, bool force_parallel) { int width_count = 0; for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { BLOBNBOX* bbox = it.data(); - TBOX box = bbox->bounding_box(); + const TBOX& box = bbox->bounding_box(); mean_width_ += box.width(); ++width_count; int x1 = IsRightTab() ? box.right() : box.left(); diff --git a/textord/textlineprojection.cpp b/textord/textlineprojection.cpp index 6018e5fda..2651a19b3 100644 --- a/textord/textlineprojection.cpp +++ b/textord/textlineprojection.cpp @@ -760,7 +760,7 @@ void TextlineProjection::TruncateToImageBounds(TPOINT* pt) const { pt->y = ClipToRange(pt->y, 0, pixGetHeight(pix_) - 1); } #ifdef _MSC_VER -#pragma optimize( "", on ) +#pragma optimize("", on) #endif // _MSC_VER // Transform tesseract image coordinates to coordinates used in the projection. diff --git a/textord/textord.cpp b/textord/textord.cpp index 1f7e8a886..94ef49c78 100644 --- a/textord/textord.cpp +++ b/textord/textord.cpp @@ -33,7 +33,8 @@ namespace tesseract { Textord::Textord(CCStruct* ccstruct) - : ccstruct_(ccstruct), use_cjk_fp_model_(false), + : ccstruct_(ccstruct), + use_cjk_fp_model_(false), // makerow.cpp /////////////////////////////////////////// BOOL_MEMBER(textord_single_height_mode, false, "Script has no xheight, so use a single mode", @@ -46,24 +47,20 @@ Textord::Textord(CCStruct* ccstruct) "old_to_method.", ccstruct_->params()), BOOL_MEMBER(tosp_only_use_prop_rows, true, - "Block stats to use fixed pitch rows?", - ccstruct_->params()), + "Block stats to use fixed pitch rows?", ccstruct_->params()), BOOL_MEMBER(tosp_force_wordbreak_on_punct, false, "Force word breaks on punct to break long lines in non-space " "delimited langs", ccstruct_->params()), - BOOL_MEMBER(tosp_use_pre_chopping, false, - "Space stats use prechopping?", + BOOL_MEMBER(tosp_use_pre_chopping, false, "Space stats use prechopping?", ccstruct_->params()), BOOL_MEMBER(tosp_old_to_bug_fix, false, "Fix suspected bug in old code", ccstruct_->params()), - BOOL_MEMBER(tosp_block_use_cert_spaces, true, - "Only stat OBVIOUS spaces", + BOOL_MEMBER(tosp_block_use_cert_spaces, true, "Only stat OBVIOUS spaces", ccstruct_->params()), BOOL_MEMBER(tosp_row_use_cert_spaces, true, "Only stat OBVIOUS spaces", ccstruct_->params()), - BOOL_MEMBER(tosp_narrow_blobs_not_cert, true, - "Only stat OBVIOUS spaces", + BOOL_MEMBER(tosp_narrow_blobs_not_cert, true, "Only stat OBVIOUS spaces", ccstruct_->params()), BOOL_MEMBER(tosp_row_use_cert_spaces1, true, "Only stat OBVIOUS spaces", ccstruct_->params()), @@ -78,30 +75,24 @@ Textord::Textord(CCStruct* ccstruct) "Don't restrict kn->sp fuzzy limit to tables", ccstruct_->params()), BOOL_MEMBER(tosp_stats_use_xht_gaps, true, - "Use within xht gap for wd breaks", - ccstruct_->params()), + "Use within xht gap for wd breaks", ccstruct_->params()), BOOL_MEMBER(tosp_use_xht_gaps, true, "Use within xht gap for wd breaks", ccstruct_->params()), BOOL_MEMBER(tosp_only_use_xht_gaps, false, - "Only use within xht gap for wd breaks", - ccstruct_->params()), + "Only use within xht gap for wd breaks", ccstruct_->params()), BOOL_MEMBER(tosp_rule_9_test_punct, false, - "Don't chng kn to space next to punct", - ccstruct_->params()), + "Don't chng kn to space next to punct", ccstruct_->params()), BOOL_MEMBER(tosp_flip_fuzz_kn_to_sp, true, "Default flip", ccstruct_->params()), BOOL_MEMBER(tosp_flip_fuzz_sp_to_kn, true, "Default flip", ccstruct_->params()), BOOL_MEMBER(tosp_improve_thresh, false, "Enable improvement heuristic", ccstruct_->params()), - INT_MEMBER(tosp_debug_level, 0, "Debug data", - ccstruct_->params()), + INT_MEMBER(tosp_debug_level, 0, "Debug data", ccstruct_->params()), INT_MEMBER(tosp_enough_space_samples_for_median, 3, - "or should we use mean", - ccstruct_->params()), + "or should we use mean", ccstruct_->params()), INT_MEMBER(tosp_redo_kern_limit, 10, - "No.samples reqd to reestimate for row", - ccstruct_->params()), + "No.samples reqd to reestimate for row", ccstruct_->params()), INT_MEMBER(tosp_few_samples, 40, "No.gaps reqd with 1 large gap to treat as a table", ccstruct_->params()), @@ -114,30 +105,24 @@ Textord::Textord(CCStruct* ccstruct) "Factor for defining space threshold in terms of space and " "kern sizes", ccstruct_->params()), - double_MEMBER(tosp_threshold_bias1, 0, - "how far between kern and space?", + double_MEMBER(tosp_threshold_bias1, 0, "how far between kern and space?", ccstruct_->params()), - double_MEMBER(tosp_threshold_bias2, 0, - "how far between kern and space?", + double_MEMBER(tosp_threshold_bias2, 0, "how far between kern and space?", ccstruct_->params()), double_MEMBER(tosp_narrow_fraction, 0.3, "Fract of xheight for narrow", ccstruct_->params()), double_MEMBER(tosp_narrow_aspect_ratio, 0.48, - "narrow if w/h less than this", - ccstruct_->params()), + "narrow if w/h less than this", ccstruct_->params()), double_MEMBER(tosp_wide_fraction, 0.52, "Fract of xheight for wide", ccstruct_->params()), double_MEMBER(tosp_wide_aspect_ratio, 0.0, "wide if w/h less than this", ccstruct_->params()), double_MEMBER(tosp_fuzzy_space_factor, 0.6, - "Fract of xheight for fuzz sp", - ccstruct_->params()), + "Fract of xheight for fuzz sp", ccstruct_->params()), double_MEMBER(tosp_fuzzy_space_factor1, 0.5, - "Fract of xheight for fuzz sp", - ccstruct_->params()), + "Fract of xheight for fuzz sp", ccstruct_->params()), double_MEMBER(tosp_fuzzy_space_factor2, 0.72, - "Fract of xheight for fuzz sp", - ccstruct_->params()), + "Fract of xheight for fuzz sp", ccstruct_->params()), double_MEMBER(tosp_gap_factor, 0.83, "gap ratio to flip sp->kern", ccstruct_->params()), double_MEMBER(tosp_kern_gap_factor1, 2.0, "gap ratio to flip kern->sp", @@ -156,14 +141,11 @@ Textord::Textord(CCStruct* ccstruct) "Fract of kerns reqd for isolated row stats", ccstruct_->params()), double_MEMBER(tosp_table_kn_sp_ratio, 2.25, - "Min difference of kn & sp in table", - ccstruct_->params()), + "Min difference of kn & sp in table", ccstruct_->params()), double_MEMBER(tosp_table_xht_sp_ratio, 0.33, - "Expect spaces bigger than this", - ccstruct_->params()), + "Expect spaces bigger than this", ccstruct_->params()), double_MEMBER(tosp_table_fuzzy_kn_sp_ratio, 3.0, - "Fuzzy if less than this", - ccstruct_->params()), + "Fuzzy if less than this", ccstruct_->params()), double_MEMBER(tosp_fuzzy_kn_fraction, 0.5, "New fuzzy kn alg", ccstruct_->params()), double_MEMBER(tosp_fuzzy_sp_fraction, 0.5, "New fuzzy sp alg", @@ -172,20 +154,16 @@ Textord::Textord(CCStruct* ccstruct) "Don't trust spaces less than this time kn", ccstruct_->params()), double_MEMBER(tosp_init_guess_kn_mult, 2.2, - "Thresh guess - mult kn by this", - ccstruct_->params()), + "Thresh guess - mult kn by this", ccstruct_->params()), double_MEMBER(tosp_init_guess_xht_mult, 0.28, - "Thresh guess - mult xht by this", - ccstruct_->params()), + "Thresh guess - mult xht by this", ccstruct_->params()), double_MEMBER(tosp_max_sane_kn_thresh, 5.0, - "Multiplier on kn to limit thresh", - ccstruct_->params()), + "Multiplier on kn to limit thresh", ccstruct_->params()), double_MEMBER(tosp_flip_caution, 0.0, "Don't autoflip kn to sp when large separation", ccstruct_->params()), double_MEMBER(tosp_large_kerning, 0.19, - "Limit use of xht gap with large kns", - ccstruct_->params()), + "Limit use of xht gap with large kns", ccstruct_->params()), double_MEMBER(tosp_dont_fool_with_small_kerns, -1, "Limit use of xht gap with odd small kns", ccstruct_->params()), @@ -193,11 +171,9 @@ Textord::Textord(CCStruct* ccstruct) "Don't reduce box if the top left is non blank", ccstruct_->params()), double_MEMBER(tosp_silly_kn_sp_gap, 0.2, - "Don't let sp minus kn get too small", - ccstruct_->params()), + "Don't let sp minus kn get too small", ccstruct_->params()), double_MEMBER(tosp_pass_wide_fuzz_sp_to_context, 0.75, - "How wide fuzzies need context", - ccstruct_->params()), + "How wide fuzzies need context", ccstruct_->params()), // tordmain.cpp /////////////////////////////////////////// BOOL_MEMBER(textord_no_rejects, false, "Don't remove noise blobs", ccstruct_->params()), @@ -206,34 +182,27 @@ Textord::Textord(CCStruct* ccstruct) BOOL_MEMBER(textord_show_boxes, false, "Display unsorted blobs", ccstruct_->params()), INT_MEMBER(textord_max_noise_size, 7, "Pixel size of noise", - ccstruct_->params()), + ccstruct_->params()), INT_MEMBER(textord_baseline_debug, 0, "Baseline debug level", - ccstruct_->params()), + ccstruct_->params()), double_MEMBER(textord_blob_size_bigile, 95, "Percentile for large blobs", ccstruct_->params()), double_MEMBER(textord_noise_area_ratio, 0.7, - "Fraction of bounding box for noise", - ccstruct_->params()), + "Fraction of bounding box for noise", ccstruct_->params()), double_MEMBER(textord_blob_size_smallile, 20, - "Percentile for small blobs", - ccstruct_->params()), + "Percentile for small blobs", ccstruct_->params()), double_MEMBER(textord_initialx_ile, 0.75, - "Ile of sizes for xheight guess", - ccstruct_->params()), + "Ile of sizes for xheight guess", ccstruct_->params()), double_MEMBER(textord_initialasc_ile, 0.90, - "Ile of sizes for xheight guess", - ccstruct_->params()), - INT_MEMBER(textord_noise_sizefraction, 10, - "Fraction of size for maxima", + "Ile of sizes for xheight guess", ccstruct_->params()), + INT_MEMBER(textord_noise_sizefraction, 10, "Fraction of size for maxima", ccstruct_->params()), double_MEMBER(textord_noise_sizelimit, 0.5, - "Fraction of x for big t count", - ccstruct_->params()), + "Fraction of x for big t count", ccstruct_->params()), INT_MEMBER(textord_noise_translimit, 16, "Transitions for normal blob", ccstruct_->params()), double_MEMBER(textord_noise_normratio, 2.0, - "Dot to norm ratio for deletion", - ccstruct_->params()), + "Dot to norm ratio for deletion", ccstruct_->params()), BOOL_MEMBER(textord_noise_rejwords, true, "Reject noise-like words", ccstruct_->params()), BOOL_MEMBER(textord_noise_rejrows, true, "Reject noise-like rows", @@ -242,24 +211,20 @@ Textord::Textord(CCStruct* ccstruct) "xh fract height error for norm blobs", ccstruct_->params()), double_MEMBER(textord_noise_sxfract, 0.4, - "xh fract width error for norm blobs", - ccstruct_->params()), - double_MEMBER(textord_noise_hfract, 1.0/64, + "xh fract width error for norm blobs", ccstruct_->params()), + double_MEMBER(textord_noise_hfract, 1.0 / 64, "Height fraction to discard outlines as speckle noise", ccstruct_->params()), INT_MEMBER(textord_noise_sncount, 1, "super norm blobs to save row", ccstruct_->params()), double_MEMBER(textord_noise_rowratio, 6.0, - "Dot to norm ratio for deletion", - ccstruct_->params()), + "Dot to norm ratio for deletion", ccstruct_->params()), BOOL_MEMBER(textord_noise_debug, false, "Debug row garbage detector", ccstruct_->params()), double_MEMBER(textord_blshift_maxshift, 0.00, "Max baseline shift", ccstruct_->params()), double_MEMBER(textord_blshift_xfraction, 9.99, - "Min size of baseline shift", - ccstruct_->params()) { -} + "Min size of baseline shift", ccstruct_->params()) {} Textord::~Textord() { } @@ -324,10 +289,9 @@ void Textord::TextordPage(PageSegMode pageseg_mode, const FCOORD& reskew, BaselineDetect baseline_detector(textord_baseline_debug, reskew, to_blocks); baseline_detector.ComputeStraightBaselines(use_box_bottoms); - baseline_detector.ComputeBaselineSplinesAndXheights(page_tr_, true, - textord_heavy_nr, - textord_show_final_rows, - this); + baseline_detector.ComputeBaselineSplinesAndXheights( + page_tr_, pageseg_mode != PSM_RAW_LINE, textord_heavy_nr, + textord_show_final_rows, this); // Now make the words in the lines. if (PSM_WORD_FIND_ENABLED(pageseg_mode)) { // SINGLE_LINE uses the old word maker on the single line. diff --git a/textord/topitch.cpp b/textord/topitch.cpp index ae9999f7d..3dda81577 100644 --- a/textord/topitch.cpp +++ b/textord/topitch.cpp @@ -1,8 +1,8 @@ /********************************************************************** * File: topitch.cpp (Formerly to_pitch.c) * Description: Code to determine fixed pitchness and the pitch if fixed. - * Author: Ray Smith - * Created: Tue Aug 24 16:57:29 BST 1993 + * Author: Ray Smith + * Created: Tue Aug 24 16:57:29 BST 1993 * * (C) Copyright 1993, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -1084,7 +1084,7 @@ BOOL8 count_pitch_stats( //find lines return FALSE; prev_valid = FALSE; prev_centre = 0; - prev_right = 0; //stop compiler warning + prev_right = 0; // stop compiler warning joined_box = blob_it.data ()->bounding_box (); do { blob_it.forward (); diff --git a/textord/tordmain.cpp b/textord/tordmain.cpp index f09a186d4..0c433a1f2 100644 --- a/textord/tordmain.cpp +++ b/textord/tordmain.cpp @@ -360,7 +360,7 @@ void Textord::cleanup_nontext_block(BLOCK* block) { // Non-text blocks must contain at least one row. ROW_IT row_it(block->row_list()); if (row_it.empty()) { - TBOX box = block->bounding_box(); + const TBOX& box = block->bounding_box(); float height = box.height(); inT32 xstarts[2] = {box.left(), box.right()}; double coeffs[3] = {0.0, 0.0, static_cast(box.bottom())}; diff --git a/textord/tospace.cpp b/textord/tospace.cpp index bec346ef5..afec42f48 100644 --- a/textord/tospace.cpp +++ b/textord/tospace.cpp @@ -419,9 +419,8 @@ void Textord::row_spacing_stats( if (suspected_table && (row->space_size < tosp_table_kn_sp_ratio * row->kern_size)) { if (tosp_debug_level > 5) - tprintf ("B:%d R:%d -- DON'T BELIEVE SPACE %3.2f %d %3.2f.\n", - block_idx, row_idx, - row->kern_size, row->space_threshold, row->space_size); + tprintf("B:%d R:%d -- DON'T BELIEVE SPACE %3.2f %d %3.2f.\n", block_idx, + row_idx, row->kern_size, row->space_threshold, row->space_size); row->space_threshold = (inT32) (tosp_table_kn_sp_ratio * row->kern_size); row->space_size = MAX (row->space_threshold + 1, row->xheight); @@ -441,10 +440,9 @@ void Textord::row_spacing_stats( MAX (tosp_min_sane_kn_sp * MAX (row->kern_size, 2.5), row->xheight / 2); if (tosp_debug_level > 5) - tprintf - ("B:%d R:%d -- DON'T BELIEVE SPACE %3.2f %d %3.2f -> %3.2f.\n", - block_idx, row_idx, row->kern_size, row->space_threshold, - row->space_size, sane_space); + tprintf("B:%d R:%d -- DON'T BELIEVE SPACE %3.2f %d %3.2f -> %3.2f.\n", + block_idx, row_idx, row->kern_size, row->space_threshold, + row->space_size, sane_space); row->space_size = sane_space; row->space_threshold = inT32 (floor ((row->space_size + row->kern_size) / @@ -455,10 +453,9 @@ void Textord::row_spacing_stats( MAX (row->kern_size, 2.5))); if (row->space_threshold > sane_threshold) { if (tosp_debug_level > 5) - tprintf ("B:%d R:%d -- DON'T BELIEVE THRESH %3.2f %d %3.2f->%d.\n", - block_idx, row_idx, - row->kern_size, - row->space_threshold, row->space_size, sane_threshold); + tprintf("B:%d R:%d -- DON'T BELIEVE THRESH %3.2f %d %3.2f->%d.\n", + block_idx, row_idx, row->kern_size, row->space_threshold, + row->space_size, sane_threshold); row->space_threshold = sane_threshold; if (row->space_size <= sane_threshold) row->space_size = row->space_threshold + 1.0f; @@ -498,7 +495,7 @@ void Textord::row_spacing_stats( MIN (inT32 (ceil (tosp_fuzzy_space_factor * row->xheight)), inT32 (row->space_size)); if (row->min_space <= row->space_threshold) - //Don't be silly + // Don't be silly row->min_space = row->space_threshold + 1; /* Lets try to guess the max certain kern gap by looking at the cluster of @@ -559,7 +556,7 @@ void Textord::row_spacing_stats( row->kern_size)); } if (row->max_nonspace > row->space_threshold) { - //Don't be silly + // Don't be silly row->max_nonspace = row->space_threshold; } @@ -700,8 +697,8 @@ BOOL8 Textord::isolated_row_stats(TO_ROW *row, ((small_gaps_count / (float) total) < tosp_enough_small_gaps) || (total - small_gaps_count < 1)) { if (tosp_debug_level > 5) - tprintf ("B:%d R:%d -- Can't do isolated row stats.\n", - block_idx, row_idx); + tprintf("B:%d R:%d -- Can't do isolated row stats.\n", block_idx, + row_idx); return FALSE; } blob_it.set_to_list (row->blob_list ()); @@ -1130,10 +1127,10 @@ ROW *Textord::make_prop_words( else blanks = 0; if (tosp_debug_level > 5) - tprintf - ("Repch wd at EOL (%d,%d). rep spacing %5.2f; Lgap:%d (%d blanks)\n", - word->bounding_box ().left (), word->bounding_box ().bottom (), - repetition_spacing, current_gap, blanks); + tprintf( + "Repch wd at EOL (%d,%d). rep spacing %5.2f; Lgap:%d (%d blanks)\n", + word->bounding_box().left(), word->bounding_box().bottom(), + repetition_spacing, current_gap, blanks); word->set_blanks (blanks); //NO uncertainty word->set_flag (W_FUZZY_SP, FALSE); @@ -1686,10 +1683,9 @@ void Textord::mark_gap( blob.bottom () + blob.height () / 2.0f); } if (tosp_debug_level > 5) - tprintf (" (%d,%d) Sp<->Kn Rule %d %d %d %d %d %d\n", - blob.left () - current_gap / 2, blob.bottom (), rule, - prev_gap, prev_blob_width, current_gap, - next_blob_width, next_gap); + tprintf(" (%d,%d) Sp<->Kn Rule %d %d %d %d %d %d\n", + blob.left() - current_gap / 2, blob.bottom(), rule, prev_gap, + prev_blob_width, current_gap, next_blob_width, next_gap); } #endif @@ -1727,8 +1723,7 @@ BOOL8 Textord::ignore_big_gap(TO_ROW *row, inT16 right) { inT16 gap = right - left + 1; - if (tosp_ignore_big_gaps > 999) - return FALSE; //Don't ignore + if (tosp_ignore_big_gaps > 999) return FALSE; // Don't ignore if (tosp_ignore_big_gaps > 0) return (gap > tosp_ignore_big_gaps * row->xheight); if (gap > tosp_ignore_very_big_gaps * row->xheight) @@ -1750,7 +1745,6 @@ BOOL8 Textord::ignore_big_gap(TO_ROW *row, return FALSE; } - /********************************************************************** * reduced_box_next * diff --git a/textord/tovars.cpp b/textord/tovars.cpp index 71114358a..6b1b83324 100644 --- a/textord/tovars.cpp +++ b/textord/tovars.cpp @@ -1,8 +1,8 @@ /********************************************************************** * File: tovars.cpp (Formerly to_vars.c) * Description: Variables used by textord. - * Author: Ray Smith - * Created: Tue Aug 24 16:55:02 BST 1993 + * Author: Ray Smith + * Created: Tue Aug 24 16:55:02 BST 1993 * * (C) Copyright 1993, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -49,8 +49,8 @@ EXTERN double_VAR (textord_words_default_minspace, 0.6, EXTERN double_VAR (textord_words_min_minspace, 0.3, "Fraction of xheight"); EXTERN double_VAR (textord_words_default_nonspace, 0.2, "Fraction of xheight"); -EXTERN double_VAR (textord_words_initial_lower, 0.25, -"Max initial cluster size"); +EXTERN double_VAR(textord_words_initial_lower, 0.25, + "Max initial cluster size"); EXTERN double_VAR (textord_words_initial_upper, 0.15, "Min initial cluster spacing"); EXTERN double_VAR (textord_words_minlarge, 0.75, @@ -67,7 +67,7 @@ EXTERN double_VAR (textord_pitch_rowsimilarity, 0.08, "Fraction of xheight for sameness"); EXTERN BOOL_VAR (textord_pitch_scalebigwords, FALSE, "Scale scores on big words"); -EXTERN double_VAR (words_initial_lower, 0.5, "Max initial cluster size"); +EXTERN double_VAR(words_initial_lower, 0.5, "Max initial cluster size"); EXTERN double_VAR (words_initial_upper, 0.15, "Min initial cluster spacing"); EXTERN double_VAR (words_default_prop_nonspace, 0.25, "Fraction of xheight"); EXTERN double_VAR (words_default_fixed_space, 0.75, "Fraction of xheight"); diff --git a/textord/tovars.h b/textord/tovars.h index 99edae1d2..46315bb96 100644 --- a/textord/tovars.h +++ b/textord/tovars.h @@ -1,8 +1,8 @@ /********************************************************************** * File: tovars.h (Formerly to_vars.h) * Description: Variables used by textord. - * Author: Ray Smith - * Created: Tue Aug 24 16:55:02 BST 1993 + * Author: Ray Smith + * Created: Tue Aug 24 16:55:02 BST 1993 * * (C) Copyright 1993, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -51,8 +51,8 @@ extern double_VAR_H (textord_words_default_minspace, 0.6, extern double_VAR_H (textord_words_min_minspace, 0.3, "Fraction of xheight"); extern double_VAR_H (textord_words_default_nonspace, 0.2, "Fraction of xheight"); -extern double_VAR_H (textord_words_initial_lower, 0.25, -"Max initial cluster size"); +extern double_VAR_H(textord_words_initial_lower, 0.25, + "Max initial cluster size"); extern double_VAR_H (textord_words_initial_upper, 0.15, "Min initial cluster spacing"); extern double_VAR_H (textord_words_minlarge, 0.75, @@ -69,7 +69,7 @@ extern double_VAR_H (textord_pitch_rowsimilarity, 0.08, "Fraction of xheight for sameness"); extern BOOL_VAR_H (textord_pitch_scalebigwords, FALSE, "Scale scores on big words"); -extern double_VAR_H (words_initial_lower, 0.5, "Max initial cluster size"); +extern double_VAR_H(words_initial_lower, 0.5, "Max initial cluster size"); extern double_VAR_H (words_initial_upper, 0.15, "Min initial cluster spacing"); extern double_VAR_H (words_default_prop_nonspace, 0.25, diff --git a/training/classifier_tester.cpp b/training/classifier_tester.cpp index 48f3781eb..ed7e50cd2 100644 --- a/training/classifier_tester.cpp +++ b/training/classifier_tester.cpp @@ -48,9 +48,9 @@ enum ClassifierName { const char* names[] = {"pruner", "full", #ifndef NO_CUBE_BUILD - "cube", "cubetess", + "cube", "cubetess", #endif // NO_CUBE_BUILD - NULL }; + NULL}; static tesseract::ShapeClassifier* InitializeClassifier( const char* classifer_name, const UNICHARSET& unicharset, @@ -80,7 +80,7 @@ static tesseract::ShapeClassifier* InitializeClassifier( tesseract::Classify* classify = NULL; if ( #ifndef NO_CUBE_BUILD - classifier == CN_CUBE || classifier == CN_CUBETESS || + classifier == CN_CUBE || classifier == CN_CUBETESS || #endif // NO_CUBE_BUILD classifier == CN_PRUNER || classifier == CN_FULL) { #ifndef NO_CUBE_BUILD diff --git a/training/cntraining.cpp b/training/cntraining.cpp index ab19ddb93..1a7986555 100644 --- a/training/cntraining.cpp +++ b/training/cntraining.cpp @@ -20,7 +20,6 @@ ** limitations under the License. ******************************************************************************/ - /*---------------------------------------------------------------------------- Include Files and Type Defines ----------------------------------------------------------------------------*/ @@ -53,10 +52,8 @@ int main ( Private Function Prototypes ----------------------------------------------------------------------------*/ -void WriteNormProtos ( - const char *Directory, - LIST LabeledProtoList, - CLUSTERER *Clusterer); +void WriteNormProtos(const char *Directory, LIST LabeledProtoList, + const FEATURE_DESC_STRUCT *feature_desc); /* PARAMDESC *ConvertToPARAMDESC( @@ -81,7 +78,6 @@ CLUSTERCONFIG CNConfig = elliptical, 0.025, 0.05, 0.8, 1e-3, 0 }; - /*---------------------------------------------------------------------------- Public Code ----------------------------------------------------------------------------*/ @@ -134,8 +130,7 @@ CLUSTERCONFIG CNConfig = * @note Exceptions: none * @note History: Fri Aug 18 08:56:17 1989, DSJ, Created. */ -int main(int argc, char* argv[]) -{ +int main(int argc, char *argv[]) { // Set the global Config parameters before parsing the command line. Config = CNConfig; diff --git a/training/commandlineflags.cpp b/training/commandlineflags.cpp index 06bfbe658..7131cba45 100644 --- a/training/commandlineflags.cpp +++ b/training/commandlineflags.cpp @@ -106,36 +106,36 @@ void PrintCommandLineFlags() { if (!strncmp(GlobalParams()->int_params[i]->name_str(), kFlagNamePrefix, kFlagNamePrefixLen)) { printf(" --%s %s (type:int default:%d)\n", - GlobalParams()->int_params[i]->name_str() + kFlagNamePrefixLen, - GlobalParams()->int_params[i]->info_str(), - inT32(*(GlobalParams()->int_params[i]))); + GlobalParams()->int_params[i]->name_str() + kFlagNamePrefixLen, + GlobalParams()->int_params[i]->info_str(), + inT32(*(GlobalParams()->int_params[i]))); } } for (int i = 0; i < GlobalParams()->double_params.size(); ++i) { if (!strncmp(GlobalParams()->double_params[i]->name_str(), kFlagNamePrefix, kFlagNamePrefixLen)) { printf(" --%s %s (type:double default:%g)\n", - GlobalParams()->double_params[i]->name_str() + kFlagNamePrefixLen, - GlobalParams()->double_params[i]->info_str(), - static_cast(*(GlobalParams()->double_params[i]))); + GlobalParams()->double_params[i]->name_str() + kFlagNamePrefixLen, + GlobalParams()->double_params[i]->info_str(), + static_cast(*(GlobalParams()->double_params[i]))); } } for (int i = 0; i < GlobalParams()->bool_params.size(); ++i) { if (!strncmp(GlobalParams()->bool_params[i]->name_str(), kFlagNamePrefix, kFlagNamePrefixLen)) { printf(" --%s %s (type:bool default:%s)\n", - GlobalParams()->bool_params[i]->name_str() + kFlagNamePrefixLen, - GlobalParams()->bool_params[i]->info_str(), - (BOOL8(*(GlobalParams()->bool_params[i])) ? "true" : "false")); + GlobalParams()->bool_params[i]->name_str() + kFlagNamePrefixLen, + GlobalParams()->bool_params[i]->info_str(), + (BOOL8(*(GlobalParams()->bool_params[i])) ? "true" : "false")); } } for (int i = 0; i < GlobalParams()->string_params.size(); ++i) { if (!strncmp(GlobalParams()->string_params[i]->name_str(), kFlagNamePrefix, kFlagNamePrefixLen)) { printf(" --%s %s (type:string default:%s)\n", - GlobalParams()->string_params[i]->name_str() + kFlagNamePrefixLen, - GlobalParams()->string_params[i]->info_str(), - GlobalParams()->string_params[i]->string()); + GlobalParams()->string_params[i]->name_str() + kFlagNamePrefixLen, + GlobalParams()->string_params[i]->info_str(), + GlobalParams()->string_params[i]->string()); } } } diff --git a/training/commontraining.cpp b/training/commontraining.cpp index 1c4cc832b..a42f986c9 100644 --- a/training/commontraining.cpp +++ b/training/commontraining.cpp @@ -39,7 +39,6 @@ #include using tesseract::CCUtil; -using tesseract::FontInfo; using tesseract::IntFeatureSpace; using tesseract::ParamUtils; using tesseract::ShapeTable; @@ -312,9 +311,7 @@ const char *GetNextFilename(int argc, const char* const * argv) { return argv[tessoptind++]; else return NULL; -} /* GetNextFilename */ - - +} /* GetNextFilename */ /*---------------------------------------------------------------------------*/ /** @@ -328,11 +325,8 @@ const char *GetNextFilename(int argc, const char* const * argv) { * @note Exceptions: none * @note History: Fri Aug 18 15:57:41 1989, DSJ, Created. */ -LABELEDLIST FindList ( - LIST List, - char *Label) -{ - LABELEDLIST LabeledList; +LABELEDLIST FindList(LIST List, char* Label) { + LABELEDLIST LabeledList; iterate (List) { @@ -342,7 +336,7 @@ LABELEDLIST FindList ( } return (NULL); -} /* FindList */ +} /* FindList */ /*---------------------------------------------------------------------------*/ /** @@ -354,10 +348,8 @@ LABELEDLIST FindList ( * @note Exceptions: none * @note History: Fri Aug 18 16:08:46 1989, DSJ, Created. */ -LABELEDLIST NewLabeledList ( - const char *Label) -{ - LABELEDLIST LabeledList; +LABELEDLIST NewLabeledList(const char* Label) { + LABELEDLIST LabeledList; LabeledList = (LABELEDLIST) Emalloc (sizeof (LABELEDLISTNODE)); LabeledList->Label = (char*)Emalloc (strlen (Label)+1); @@ -367,7 +359,7 @@ LABELEDLIST NewLabeledList ( LabeledList->font_sample_count = 0; return (LabeledList); -} /* NewLabeledList */ +} /* NewLabeledList */ /*---------------------------------------------------------------------------*/ // TODO(rays) This is now used only by cntraining. Convert cntraining to use @@ -386,7 +378,7 @@ LABELEDLIST NewLabeledList ( * @return none * @note Globals: none * @note Exceptions: none - * @note History: + * @note History: * - Fri Aug 18 13:11:39 1989, DSJ, Created. * - Tue May 17 1998 simplifications to structure, illiminated * font, and feature specification levels of structure. @@ -460,17 +452,17 @@ void FreeTrainingSamples(LIST CharList) { FEATURE_SET FeatureSet; LIST FeatureList; - - iterate(CharList) { /* iterate through all of the fonts */ + LIST nodes = CharList; + iterate(CharList) { /* iterate through all of the fonts */ char_sample = (LABELEDLIST) first_node(CharList); FeatureList = char_sample->List; - iterate(FeatureList) { /* iterate through all of the classes */ + iterate(FeatureList) { /* iterate through all of the classes */ FeatureSet = (FEATURE_SET) first_node(FeatureList); FreeFeatureSet(FeatureSet); } FreeLabeledList(char_sample); } - destroy(CharList); + destroy(nodes); } /* FreeTrainingSamples */ /*---------------------------------------------------------------------------*/ @@ -535,12 +527,12 @@ CLUSTERER *SetUpForClustering(const FEATURE_DEFS_STRUCT &FeatureDefs, if ( Sample != NULL ) free( Sample ); return( Clusterer ); -} /* SetUpForClustering */ +} /* SetUpForClustering */ /*------------------------------------------------------------------------*/ void MergeInsignificantProtos(LIST ProtoList, const char* label, - CLUSTERER *Clusterer, CLUSTERCONFIG *Config) { - PROTOTYPE *Prototype; + CLUSTERER* Clusterer, CLUSTERCONFIG* Config) { + PROTOTYPE* Prototype; bool debug = strcmp(FLAGS_test_ch.c_str(), label) == 0; LIST pProtoList = ProtoList; @@ -600,7 +592,7 @@ void MergeInsignificantProtos(LIST ProtoList, const char* label, Prototype->Significant = true; } } -} /* MergeInsignificantProtos */ +} /* MergeInsignificantProtos */ /*-----------------------------------------------------------------------------*/ void CleanUpUnusedData( @@ -695,14 +687,11 @@ LIST RemoveInsignificantProtos( } FreeProtoList(&ProtoList); return (NewProtoList); -} /* RemoveInsignificantProtos */ +} /* RemoveInsignificantProtos */ /*----------------------------------------------------------------------------*/ -MERGE_CLASS FindClass ( - LIST List, - const char *Label) -{ - MERGE_CLASS MergeClass; +MERGE_CLASS FindClass(LIST List, const char* Label) { + MERGE_CLASS MergeClass; iterate (List) { @@ -712,13 +701,11 @@ MERGE_CLASS FindClass ( } return (NULL); -} /* FindClass */ +} /* FindClass */ /*---------------------------------------------------------------------------*/ -MERGE_CLASS NewLabeledClass ( - const char *Label) -{ - MERGE_CLASS MergeClass; +MERGE_CLASS NewLabeledClass(const char* Label) { + MERGE_CLASS MergeClass; MergeClass = new MERGE_CLASS_NODE; MergeClass->Label = (char*)Emalloc (strlen (Label)+1); @@ -726,7 +713,7 @@ MERGE_CLASS NewLabeledClass ( MergeClass->Class = NewClass (MAX_NUM_PROTOS, MAX_NUM_CONFIGS); return (MergeClass); -} /* NewLabeledClass */ +} /* NewLabeledClass */ /*-----------------------------------------------------------------------------*/ /** @@ -738,38 +725,37 @@ MERGE_CLASS NewLabeledClass ( * @note Exceptions: none * @note History: Fri Aug 18 17:44:27 1989, DSJ, Created. */ -void FreeLabeledClassList ( - LIST ClassList) -{ - MERGE_CLASS MergeClass; +void FreeLabeledClassList(LIST ClassList) { + MERGE_CLASS MergeClass; - iterate (ClassList) /* iterate through all of the fonts */ + LIST nodes = ClassList; + iterate(ClassList) /* iterate through all of the fonts */ { MergeClass = (MERGE_CLASS) first_node (ClassList); free (MergeClass->Label); FreeClass(MergeClass->Class); delete MergeClass; } - destroy (ClassList); + destroy(nodes); -} /* FreeLabeledClassList */ +} /* FreeLabeledClassList */ /* SetUpForFloat2Int */ CLASS_STRUCT* SetUpForFloat2Int(const UNICHARSET& unicharset, LIST LabeledClassList) { - MERGE_CLASS MergeClass; - CLASS_TYPE Class; - int NumProtos; - int NumConfigs; - int NumWords; - int i, j; - float Values[3]; - PROTO NewProto; - PROTO OldProto; - BIT_VECTOR NewConfig; - BIT_VECTOR OldConfig; + MERGE_CLASS MergeClass; + CLASS_TYPE Class; + int NumProtos; + int NumConfigs; + int NumWords; + int i, j; + float Values[3]; + PROTO NewProto; + PROTO OldProto; + BIT_VECTOR NewConfig; + BIT_VECTOR OldConfig; - // printf("Float2Int ...\n"); + // printf("Float2Int ...\n"); CLASS_STRUCT* float_classes = new CLASS_STRUCT[unicharset.size()]; iterate(LabeledClassList) @@ -835,20 +821,20 @@ void Normalize ( } // Normalize /*-------------------------------------------------------------------------*/ -void FreeNormProtoList ( - LIST CharList) +void FreeNormProtoList(LIST CharList) { - LABELEDLIST char_sample; + LABELEDLIST char_sample; - iterate (CharList) /* iterate through all of the fonts */ + LIST nodes = CharList; + iterate(CharList) /* iterate through all of the fonts */ { char_sample = (LABELEDLIST) first_node (CharList); FreeLabeledList (char_sample); } - destroy (CharList); + destroy(nodes); -} // FreeNormProtoList +} // FreeNormProtoList /*---------------------------------------------------------------------------*/ void AddToNormProtosList( @@ -869,19 +855,16 @@ void AddToNormProtosList( } /*---------------------------------------------------------------------------*/ -int NumberOfProtos( - LIST ProtoList, - BOOL8 CountSigProtos, - BOOL8 CountInsigProtos) -{ +int NumberOfProtos(LIST ProtoList, BOOL8 CountSigProtos, + BOOL8 CountInsigProtos) { int N = 0; - PROTOTYPE *Proto; + PROTOTYPE* Proto; iterate(ProtoList) { Proto = (PROTOTYPE *) first_node ( ProtoList ); - if (( Proto->Significant && CountSigProtos ) || - ( ! Proto->Significant && CountInsigProtos ) ) + if ((Proto->Significant && CountSigProtos) || + (!Proto->Significant && CountInsigProtos)) N++; } return(N); diff --git a/training/fileio.cpp b/training/fileio.cpp index e3e43bd02..bb1f4afce 100644 --- a/training/fileio.cpp +++ b/training/fileio.cpp @@ -81,8 +81,9 @@ bool File::ReadFileToString(const string& filename, string* out) { } string File::JoinPath(const string& prefix, const string& suffix) { - return (!prefix.size() || prefix[prefix.size() - 1] == '/') ? - prefix + suffix : prefix + "/" + suffix; + return (prefix.empty() || prefix[prefix.size() - 1] == '/') + ? prefix + suffix + : prefix + "/" + suffix; } bool File::Delete(const char* pathname) { diff --git a/training/mftraining.cpp b/training/mftraining.cpp index 60314a1cd..eabcbf32d 100644 --- a/training/mftraining.cpp +++ b/training/mftraining.cpp @@ -64,9 +64,6 @@ #include "tprintf.h" #include "unicity_table.h" -using tesseract::Classify; -using tesseract::FontInfo; -using tesseract::FontSpacingInfo; using tesseract::IndexMapBiDi; using tesseract::MasterTrainer; using tesseract::Shape; diff --git a/training/pango_font_info.cpp b/training/pango_font_info.cpp index c2b508c02..6a009f7b8 100644 --- a/training/pango_font_info.cpp +++ b/training/pango_font_info.cpp @@ -437,10 +437,15 @@ bool PangoFontInfo::CanRenderString(const char* utf8_word, int len, PangoGlyph dotted_circle_glyph; PangoFont* font = run->item->analysis.font; - PangoGlyphString * glyphs = pango_glyph_string_new(); +#ifdef _WIN32 // Fixme! Leaks memory and breaks unittests. + PangoGlyphString* glyphs = pango_glyph_string_new(); char s[] = "\xc2\xa7"; pango_shape(s, sizeof(s), &(run->item->analysis), glyphs); dotted_circle_glyph = glyphs->glyphs[0].glyph; +#else + dotted_circle_glyph = pango_fc_font_get_glyph( + reinterpret_cast(font), kDottedCircleGlyph); +#endif if (TLOG_IS_ON(2)) { PangoFontDescription* desc = pango_font_describe(font); @@ -519,17 +524,16 @@ vector FontUtils::available_fonts_; // cache list bool FontUtils::IsAvailableFont(const char* input_query_desc, string* best_match) { string query_desc(input_query_desc); - if (PANGO_VERSION <= 12005) { - // Strip commas and any ' Medium' substring in the name. - query_desc.erase(std::remove(query_desc.begin(), query_desc.end(), ','), - query_desc.end()); - const string kMediumStr = " Medium"; - std::size_t found = query_desc.find(kMediumStr); - if (found != std::string::npos) { - query_desc.erase(found, kMediumStr.length()); - } +#if (PANGO_VERSION <= 12005) + // Strip commas and any ' Medium' substring in the name. + query_desc.erase(std::remove(query_desc.begin(), query_desc.end(), ','), + query_desc.end()); + const string kMediumStr = " Medium"; + std::size_t found = query_desc.find(kMediumStr); + if (found != std::string::npos) { + query_desc.erase(found, kMediumStr.length()); } - +#endif PangoFontDescription *desc = pango_font_description_from_string( query_desc.c_str()); PangoFont* selected_font = NULL; @@ -589,7 +593,7 @@ static bool ShouldIgnoreFontFamilyName(const char* query) { // Outputs description names of available fonts. /* static */ const vector& FontUtils::ListAvailableFonts() { - if (available_fonts_.size()) { + if (!available_fonts_.empty()) { return available_fonts_; } #ifndef USE_STD_NAMESPACE diff --git a/training/pango_font_info.h b/training/pango_font_info.h index 421139a16..f07d712f1 100644 --- a/training/pango_font_info.h +++ b/training/pango_font_info.h @@ -93,15 +93,15 @@ class PangoFontInfo { // Font Family name eg. "Arial" const string& family_name() const { return family_name_; } // Size in points (1/72"), rounded to the nearest integer. - int font_size() const { return font_size_; } - bool is_bold() const { return is_bold_; } - bool is_italic() const { return is_italic_; } - bool is_smallcaps() const { return is_smallcaps_; } - bool is_monospace() const { return is_monospace_; } - bool is_fraktur() const { return is_fraktur_; } + int font_size() const { return font_size_; } + bool is_bold() const { return is_bold_; } + bool is_italic() const { return is_italic_; } + bool is_smallcaps() const { return is_smallcaps_; } + bool is_monospace() const { return is_monospace_; } + bool is_fraktur() const { return is_fraktur_; } FontTypeEnum font_type() const { return font_type_; } - int resolution() const { return resolution_; } + int resolution() const { return resolution_; } void set_resolution(const int resolution) { resolution_ = resolution; } diff --git a/training/stringrenderer.cpp b/training/stringrenderer.cpp index fbff55fb1..225f5e44a 100644 --- a/training/stringrenderer.cpp +++ b/training/stringrenderer.cpp @@ -52,7 +52,7 @@ static const int kDefaultOutputResolution = 300; // Word joiner (U+2060) inserted after letters in ngram mode, as per // recommendation in http://unicode.org/reports/tr14/ to avoid line-breaks at // hyphens and other non-alpha characters. -static const char* kWordJoinerUTF8 = "\xE2\x81\xA0"; //u8"\u2060"; +static const char* kWordJoinerUTF8 = "\xE2\x81\xA0"; // u8"\u2060"; static const char32 kWordJoiner = 0x2060; static bool IsCombiner(int ch) { @@ -209,8 +209,7 @@ void StringRenderer::SetLayoutProperties() { #if (PANGO_VERSION_MAJOR == 1 && PANGO_VERSION_MINOR >= 38) if (add_ligatures_) { set_features("liga, clig, dlig, hlig"); - PangoAttribute* feature_attr = - pango_attr_font_features_new(features_); + PangoAttribute* feature_attr = pango_attr_font_features_new(features_); pango_attr_list_change(attr_list, feature_attr); } #endif @@ -396,7 +395,7 @@ bool StringRenderer::GetClusterStrings(vector* cluster_text) { it != start_byte_to_text.end(); ++it) { cluster_text->push_back(it->second); } - return cluster_text->size(); + return !cluster_text->empty(); } // Merges an array of BoxChars into words based on the identification of @@ -496,7 +495,7 @@ void StringRenderer::ComputeClusterBoxes() { const int end_byte_index = cluster_start_to_end_index[start_byte_index]; string cluster_text = string(text + start_byte_index, end_byte_index - start_byte_index); - if (cluster_text.size() && cluster_text[0] == '\n') { + if (!cluster_text.empty() && cluster_text[0] == '\n') { tlog(2, "Skipping newlines at start of text.\n"); continue; } @@ -596,11 +595,12 @@ void StringRenderer::ComputeClusterBoxes() { all_boxes = boxaCreate(0); boxaAddBox(all_boxes, page_boxchars[i]->mutable_box(), L_CLONE); } - boxaGetExtent(all_boxes, NULL, NULL, &page_box); - boxaDestroy(&all_boxes); - if (page_boxes_ == NULL) - page_boxes_ = boxaCreate(0); - boxaAddBox(page_boxes_, page_box, L_INSERT); + if (all_boxes != NULL) { + boxaGetExtent(all_boxes, NULL, NULL, &page_box); + boxaDestroy(&all_boxes); + if (page_boxes_ == NULL) page_boxes_ = boxaCreate(0); + boxaAddBox(page_boxes_, page_box, L_INSERT); + } } diff --git a/training/tesstrain_utils.sh b/training/tesstrain_utils.sh index c45d00378..ae6a97b99 100755 --- a/training/tesstrain_utils.sh +++ b/training/tesstrain_utils.sh @@ -90,8 +90,8 @@ parse_flags() { --) break;; --fontlist) - fn=0 - FONTS="" + fn=0 + FONTS="" while test $j -lt ${#ARGV[@]}; do test -z "${ARGV[$j]}" && break test `echo ${ARGV[$j]} | cut -c -2` = "--" && break diff --git a/training/text2image.cpp b/training/text2image.cpp index 406669dc1..946f6facd 100644 --- a/training/text2image.cpp +++ b/training/text2image.cpp @@ -251,6 +251,8 @@ void ExtractFontProperties(const string &utf8_text, // the input consists of the separated characters. NOTE(ranjith): As per // behdad@ this is not currently controllable at the level of the Pango // API. + // The most frequent of all is a single character "word" made by the CJK + // segmenter. // Safeguard against these cases here by just skipping the bigram. if (IsWhitespaceBox(boxes[b+1])) { continue; @@ -445,7 +447,7 @@ int main(int argc, char** argv) { string pango_name; if (!FontUtils::IsAvailableFont(FLAGS_font.c_str(), &pango_name)) { tprintf("Could not find font named %s.\n", FLAGS_font.c_str()); - if (!pango_name.empty()) { + if (!pango_name.empty()) { tprintf("Pango suggested font %s.\n", pango_name.c_str()); } tprintf("Please correct --font arg.\n"); @@ -523,7 +525,7 @@ int main(int argc, char** argv) { if (FLAGS_render_ngrams && !FLAGS_unicharset_file.empty() && !unicharset.load_from_file(FLAGS_unicharset_file.c_str())) { tprintf("Failed to load unicharset from file %s\n", - FLAGS_unicharset_file.c_str()); + FLAGS_unicharset_file.c_str()); exit(1); } @@ -604,7 +606,8 @@ int main(int argc, char** argv) { rotation = -1 * page_rotation[page_num]; } if (FLAGS_degrade_image) { - pix = DegradeImage(pix, FLAGS_exposure, &randomizer, FLAGS_rotate_image ? &rotation : NULL); + pix = DegradeImage(pix, FLAGS_exposure, &randomizer, + FLAGS_rotate_image ? &rotation : NULL); } render.RotatePageBoxes(rotation); diff --git a/viewer/scrollview.h b/viewer/scrollview.h index 12fac4e5d..e0537422b 100644 --- a/viewer/scrollview.h +++ b/viewer/scrollview.h @@ -327,7 +327,7 @@ class ScrollView { // be unique among menubar eventIDs. void MenuItem(const char* parent, const char* name, int cmdEvent); -// This adds a new checkbox entry, which might initially be flagged. + // This adds a new checkbox entry, which might initially be flagged. void MenuItem(const char* parent, const char* name, int cmdEvent, bool flagged); diff --git a/viewer/svutil.cpp b/viewer/svutil.cpp index 80e11019e..4104a189c 100644 --- a/viewer/svutil.cpp +++ b/viewer/svutil.cpp @@ -31,13 +31,13 @@ struct addrinfo { }; #else #include +#include #include #include #include #include #include #include -#include #include #include #ifdef __linux__ @@ -56,10 +56,34 @@ struct addrinfo { #include "config_auto.h" #endif -#ifndef GRAPHICS_DISABLED - #include "svutil.h" +SVMutex::SVMutex() { +#ifdef _WIN32 + mutex_ = CreateMutex(0, FALSE, 0); +#else + pthread_mutex_init(&mutex_, NULL); +#endif +} + +void SVMutex::Lock() { +#ifdef _WIN32 + WaitForSingleObject(mutex_, INFINITE); +#else + pthread_mutex_lock(&mutex_); +#endif +} + +void SVMutex::Unlock() { +#ifdef _WIN32 + ReleaseMutex(mutex_); +#else + pthread_mutex_unlock(&mutex_); +#endif +} + +#ifndef GRAPHICS_DISABLED + const int kMaxMsgSize = 4096; // Signals a thread to exit. @@ -161,29 +185,6 @@ void SVSemaphore::Wait() { #endif } -SVMutex::SVMutex() { -#ifdef _WIN32 - mutex_ = CreateMutex(0, FALSE, 0); -#else - pthread_mutex_init(&mutex_, NULL); -#endif -} - -void SVMutex::Lock() { -#ifdef _WIN32 - WaitForSingleObject(mutex_, INFINITE); -#else - pthread_mutex_lock(&mutex_); -#endif -} - -void SVMutex::Unlock() { -#ifdef _WIN32 - ReleaseMutex(mutex_); -#else - pthread_mutex_unlock(&mutex_); -#endif -} // Create new thread. @@ -200,7 +201,10 @@ void SVSync::StartThread(void *(*func)(void*), void* arg) { &threadid); // returns the thread identifier #else pthread_t helper; - pthread_create(&helper, NULL, func, arg); + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + pthread_create(&helper, &attr, func, arg); #endif } @@ -214,7 +218,7 @@ void SVNetwork::Send(const char* msg) { // Send the whole buffer. void SVNetwork::Flush() { mutex_send_->Lock(); - while (msg_buffer_out_.size() > 0) { + while (!msg_buffer_out_.empty()) { int i = send(stream_, msg_buffer_out_.c_str(), msg_buffer_out_.length(), 0); msg_buffer_out_.erase(0, i); } @@ -302,7 +306,8 @@ static std::string ScrollViewCommand(std::string scrollview_path) { const char* cmd_template = "-Djava.library.path=%s -jar %s/ScrollView.jar"; #else - const char* cmd_template = "-c \"trap 'kill %%1' 0 1 2 ; java " + const char* cmd_template = + "-c \"trap 'kill %%1' 0 1 2 ; java " "-Xms1024m -Xmx2048m -jar %s/ScrollView.jar" " & wait\""; #endif diff --git a/wordrec/associate.h b/wordrec/associate.h index 3d6fc4470..10b1e0b7d 100644 --- a/wordrec/associate.h +++ b/wordrec/associate.h @@ -47,9 +47,7 @@ struct AssociateStats { gap_sum = 0; } - void Print() { - tprintf("AssociateStats: w(%g %d)\n", shape_cost, bad_shape); - } + void Print() { tprintf("AssociateStats: s(%g %d)\n", shape_cost, bad_shape); } float shape_cost; // cost of blob shape bool bad_shape; // true if the shape of the blob is unacceptable diff --git a/wordrec/lm_state.h b/wordrec/lm_state.h index 623bbb5e7..6229e9b35 100644 --- a/wordrec/lm_state.h +++ b/wordrec/lm_state.h @@ -48,8 +48,8 @@ typedef unsigned char LanguageModelFlagsType; /// Each ViterbiStateEntry contains information from various components of the /// language model: dawgs in which the path is found, character ngram model /// probability of the path, script/chartype/font consistency info, state for -/// language-specific heuristics (e.g. hyphenated and compound words, lower/upper -/// case preferences, etc). +/// language-specific heuristics (e.g. hyphenated and compound words, +/// lower/upper case preferences, etc). /// /// Each ViterbiStateEntry also contains the parent pointer, so that the path /// that it represents (WERD_CHOICE) can be constructed by following these @@ -165,13 +165,13 @@ struct ViterbiStateEntry : public ELIST_LINK { /// Various information about the characters on the path represented /// by this ViterbiStateEntry. - float ratings_sum; //< sum of ratings of character on the path - float min_certainty; //< minimum certainty on the path - int adapted; //< number of BLOB_CHOICES from adapted templates - int length; //< number of characters on the path + float ratings_sum; //< sum of ratings of character on the path + float min_certainty; //< minimum certainty on the path + int adapted; //< number of BLOB_CHOICES from adapted templates + int length; //< number of characters on the path float outline_length; //< length of the outline so far LMConsistencyInfo consistency_info; //< path consistency info - AssociateStats associate_stats; //< character widths/gaps/seams + AssociateStats associate_stats; //< character widths/gaps/seams /// Flags for marking the entry as a top choice path with /// the smallest rating or lower/upper case letters). diff --git a/wordrec/measure.h b/wordrec/measure.h index 9c7390685..894938e55 100644 --- a/wordrec/measure.h +++ b/wordrec/measure.h @@ -60,10 +60,9 @@ typedef struct * Add one more sample to a measurement. **********************************************************************/ -#define ADD_SAMPLE(m,s) \ -(m.sum_of_samples += (float) (s), \ - m.sum_of_squares += (float) (s) * (float) (s), \ - ++m.num_samples) +#define ADD_SAMPLE(m, s) \ + (m.sum_of_samples += (float)(s), \ + m.sum_of_squares += (float)(s) * (float)(s), ++m.num_samples) /********************************************************************** * mean @@ -71,10 +70,8 @@ typedef struct * Return the mean value of the measurement. **********************************************************************/ -#define MEAN(m) \ -((m).num_samples ? \ - ((float) ((m).sum_of_samples / (m).num_samples)) : \ - 0) +#define MEAN(m) \ + ((m).num_samples ? ((float)((m).sum_of_samples / (m).num_samples)) : 0) /********************************************************************** * new_measurement @@ -83,10 +80,8 @@ typedef struct * samples. **********************************************************************/ -#define new_measurement(m) \ -((m).num_samples = 0, \ - (m).sum_of_samples = 0, \ - (m).sum_of_squares = 0) +#define new_measurement(m) \ + ((m).num_samples = 0, (m).sum_of_samples = 0, (m).sum_of_squares = 0) /********************************************************************** * number_of_samples @@ -112,13 +107,12 @@ typedef struct * Return the variance of the measurement. **********************************************************************/ -#define VARIANCE(m) \ -(((m).num_samples > 1) ? \ - ((float) \ - (((m).num_samples * (m).sum_of_squares - \ - (m).sum_of_samples * (m).sum_of_samples) / \ - (((m).num_samples - 1) * (m).num_samples))) : \ - 0) +#define VARIANCE(m) \ + (((m).num_samples > 1) \ + ? ((float)(((m).num_samples * (m).sum_of_squares - \ + (m).sum_of_samples * (m).sum_of_samples) / \ + (((m).num_samples - 1) * (m).num_samples))) \ + : 0) /********************************************************************** * print_summary @@ -126,10 +120,8 @@ typedef struct * Summarize a MEASUREMENT record. **********************************************************************/ -#define print_summary(string,measure) \ -cprintf ("\t%-20s \tn = %d, \tm = %4.2f, \ts = %4.2f\n ", \ - string, \ - number_of_samples (measure), \ - MEAN (measure), \ - standard_deviation (measure)) +#define print_summary(string, measure) \ + cprintf("\t%-20s \tn = %d, \tm = %4.2f, \ts = %4.2f\n ", string, \ + number_of_samples(measure), MEAN(measure), \ + standard_deviation(measure)) #endif diff --git a/wordrec/pieces.cpp b/wordrec/pieces.cpp index 04e340396..f7b406d5b 100644 --- a/wordrec/pieces.cpp +++ b/wordrec/pieces.cpp @@ -267,7 +267,6 @@ void Wordrec::merge_and_put_fragment_lists(inT16 row, inT16 column, delete [] choice_lists_it; } - /********************************************************************** * get_fragment_lists *