1 ///////////////////////////////////////////////////////////////////////
2 // File:        capi.cpp
3 // Description: C-API TessBaseAPI
4 //
5 // (C) Copyright 2012, Google Inc.
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 ///////////////////////////////////////////////////////////////////////
17 
18 #include <tesseract/capi.h>
19 
20 #include <cstring> // for strdup
21 
TessVersion()22 const char *TessVersion() {
23   return TessBaseAPI::Version();
24 }
25 
TessDeleteText(const char * text)26 void TessDeleteText(const char *text) {
27   delete[] text;
28 }
29 
TessDeleteTextArray(char ** arr)30 void TessDeleteTextArray(char **arr) {
31   for (char **pos = arr; *pos != nullptr; ++pos) {
32     delete[] * pos;
33   }
34   delete[] arr;
35 }
36 
TessDeleteIntArray(const int * arr)37 void TessDeleteIntArray(const int *arr) {
38   delete[] arr;
39 }
40 
TessTextRendererCreate(const char * outputbase)41 TessResultRenderer *TessTextRendererCreate(const char *outputbase) {
42   return new tesseract::TessTextRenderer(outputbase);
43 }
44 
TessHOcrRendererCreate(const char * outputbase)45 TessResultRenderer *TessHOcrRendererCreate(const char *outputbase) {
46   return new tesseract::TessHOcrRenderer(outputbase);
47 }
48 
TessHOcrRendererCreate2(const char * outputbase,BOOL font_info)49 TessResultRenderer *TessHOcrRendererCreate2(const char *outputbase, BOOL font_info) {
50   return new tesseract::TessHOcrRenderer(outputbase, font_info != 0);
51 }
52 
TessAltoRendererCreate(const char * outputbase)53 TessResultRenderer *TessAltoRendererCreate(const char *outputbase) {
54   return new tesseract::TessAltoRenderer(outputbase);
55 }
56 
TessTsvRendererCreate(const char * outputbase)57 TessResultRenderer *TessTsvRendererCreate(const char *outputbase) {
58   return new tesseract::TessTsvRenderer(outputbase);
59 }
60 
TessPDFRendererCreate(const char * outputbase,const char * datadir,BOOL textonly)61 TessResultRenderer *TessPDFRendererCreate(const char *outputbase, const char *datadir,
62                                           BOOL textonly) {
63   return new tesseract::TessPDFRenderer(outputbase, datadir, textonly != 0);
64 }
65 
TessUnlvRendererCreate(const char * outputbase)66 TessResultRenderer *TessUnlvRendererCreate(const char *outputbase) {
67   return new tesseract::TessUnlvRenderer(outputbase);
68 }
69 
TessBoxTextRendererCreate(const char * outputbase)70 TessResultRenderer *TessBoxTextRendererCreate(const char *outputbase) {
71   return new tesseract::TessBoxTextRenderer(outputbase);
72 }
73 
TessWordStrBoxRendererCreate(const char * outputbase)74 TessResultRenderer *TessWordStrBoxRendererCreate(const char *outputbase) {
75   return new tesseract::TessWordStrBoxRenderer(outputbase);
76 }
77 
TessLSTMBoxRendererCreate(const char * outputbase)78 TessResultRenderer *TessLSTMBoxRendererCreate(const char *outputbase) {
79   return new tesseract::TessLSTMBoxRenderer(outputbase);
80 }
81 
TessDeleteResultRenderer(TessResultRenderer * renderer)82 void TessDeleteResultRenderer(TessResultRenderer *renderer) {
83   delete renderer;
84 }
85 
TessResultRendererInsert(TessResultRenderer * renderer,TessResultRenderer * next)86 void TessResultRendererInsert(TessResultRenderer *renderer, TessResultRenderer *next) {
87   renderer->insert(next);
88 }
89 
TessResultRendererNext(TessResultRenderer * renderer)90 TessResultRenderer *TessResultRendererNext(TessResultRenderer *renderer) {
91   return renderer->next();
92 }
93 
TessResultRendererBeginDocument(TessResultRenderer * renderer,const char * title)94 BOOL TessResultRendererBeginDocument(TessResultRenderer *renderer, const char *title) {
95   return static_cast<int>(renderer->BeginDocument(title));
96 }
97 
TessResultRendererAddImage(TessResultRenderer * renderer,TessBaseAPI * api)98 BOOL TessResultRendererAddImage(TessResultRenderer *renderer, TessBaseAPI *api) {
99   return static_cast<int>(renderer->AddImage(api));
100 }
101 
TessResultRendererEndDocument(TessResultRenderer * renderer)102 BOOL TessResultRendererEndDocument(TessResultRenderer *renderer) {
103   return static_cast<int>(renderer->EndDocument());
104 }
105 
TessResultRendererExtention(TessResultRenderer * renderer)106 const char *TessResultRendererExtention(TessResultRenderer *renderer) {
107   return renderer->file_extension();
108 }
109 
TessResultRendererTitle(TessResultRenderer * renderer)110 const char *TessResultRendererTitle(TessResultRenderer *renderer) {
111   return renderer->title();
112 }
113 
TessResultRendererImageNum(TessResultRenderer * renderer)114 int TessResultRendererImageNum(TessResultRenderer *renderer) {
115   return renderer->imagenum();
116 }
117 
TessBaseAPICreate()118 TessBaseAPI *TessBaseAPICreate() {
119   return new TessBaseAPI;
120 }
121 
TessBaseAPIDelete(TessBaseAPI * handle)122 void TessBaseAPIDelete(TessBaseAPI *handle) {
123   delete handle;
124 }
125 
TessBaseAPIGetOpenCLDevice(TessBaseAPI *,void ** device)126 size_t TessBaseAPIGetOpenCLDevice(TessBaseAPI * /*handle*/, void **device) {
127   return TessBaseAPI::getOpenCLDevice(device);
128 }
129 
TessBaseAPISetInputName(TessBaseAPI * handle,const char * name)130 void TessBaseAPISetInputName(TessBaseAPI *handle, const char *name) {
131   handle->SetInputName(name);
132 }
133 
TessBaseAPIGetInputName(TessBaseAPI * handle)134 const char *TessBaseAPIGetInputName(TessBaseAPI *handle) {
135   return handle->GetInputName();
136 }
137 
TessBaseAPISetInputImage(TessBaseAPI * handle,Pix * pix)138 void TessBaseAPISetInputImage(TessBaseAPI *handle, Pix *pix) {
139   handle->SetInputImage(pix);
140 }
141 
TessBaseAPIGetInputImage(TessBaseAPI * handle)142 Pix *TessBaseAPIGetInputImage(TessBaseAPI *handle) {
143   return handle->GetInputImage();
144 }
145 
TessBaseAPIGetSourceYResolution(TessBaseAPI * handle)146 int TessBaseAPIGetSourceYResolution(TessBaseAPI *handle) {
147   return handle->GetSourceYResolution();
148 }
149 
TessBaseAPIGetDatapath(TessBaseAPI * handle)150 const char *TessBaseAPIGetDatapath(TessBaseAPI *handle) {
151   return handle->GetDatapath();
152 }
153 
TessBaseAPISetOutputName(TessBaseAPI * handle,const char * name)154 void TessBaseAPISetOutputName(TessBaseAPI *handle, const char *name) {
155   handle->SetOutputName(name);
156 }
157 
TessBaseAPISetVariable(TessBaseAPI * handle,const char * name,const char * value)158 BOOL TessBaseAPISetVariable(TessBaseAPI *handle, const char *name, const char *value) {
159   return static_cast<int>(handle->SetVariable(name, value));
160 }
161 
TessBaseAPISetDebugVariable(TessBaseAPI * handle,const char * name,const char * value)162 BOOL TessBaseAPISetDebugVariable(TessBaseAPI *handle, const char *name, const char *value) {
163   return static_cast<int>(handle->SetDebugVariable(name, value));
164 }
165 
TessBaseAPIGetIntVariable(const TessBaseAPI * handle,const char * name,int * value)166 BOOL TessBaseAPIGetIntVariable(const TessBaseAPI *handle, const char *name, int *value) {
167   return static_cast<int>(handle->GetIntVariable(name, value));
168 }
169 
TessBaseAPIGetBoolVariable(const TessBaseAPI * handle,const char * name,BOOL * value)170 BOOL TessBaseAPIGetBoolVariable(const TessBaseAPI *handle, const char *name, BOOL *value) {
171   bool boolValue;
172   bool result = handle->GetBoolVariable(name, &boolValue);
173   if (result) {
174     *value = static_cast<int>(boolValue);
175   }
176   return static_cast<int>(result);
177 }
178 
TessBaseAPIGetDoubleVariable(const TessBaseAPI * handle,const char * name,double * value)179 BOOL TessBaseAPIGetDoubleVariable(const TessBaseAPI *handle, const char *name, double *value) {
180   return static_cast<int>(handle->GetDoubleVariable(name, value));
181 }
182 
TessBaseAPIGetStringVariable(const TessBaseAPI * handle,const char * name)183 const char *TessBaseAPIGetStringVariable(const TessBaseAPI *handle, const char *name) {
184   return handle->GetStringVariable(name);
185 }
186 
TessBaseAPIPrintVariables(const TessBaseAPI * handle,FILE * fp)187 void TessBaseAPIPrintVariables(const TessBaseAPI *handle, FILE *fp) {
188   handle->PrintVariables(fp);
189 }
190 
TessBaseAPIPrintVariablesToFile(const TessBaseAPI * handle,const char * filename)191 BOOL TessBaseAPIPrintVariablesToFile(const TessBaseAPI *handle, const char *filename) {
192   FILE *fp = fopen(filename, "w");
193   if (fp != nullptr) {
194     handle->PrintVariables(fp);
195     fclose(fp);
196     return TRUE;
197   }
198   return FALSE;
199 }
200 
TessBaseAPIInit4(TessBaseAPI * handle,const char * datapath,const char * language,TessOcrEngineMode mode,char ** configs,int configs_size,char ** vars_vec,char ** vars_values,size_t vars_vec_size,BOOL set_only_non_debug_params)201 int TessBaseAPIInit4(TessBaseAPI *handle, const char *datapath, const char *language,
202                      TessOcrEngineMode mode, char **configs, int configs_size, char **vars_vec,
203                      char **vars_values, size_t vars_vec_size, BOOL set_only_non_debug_params) {
204   std::vector<std::string> varNames;
205   std::vector<std::string> varValues;
206   if (vars_vec != nullptr && vars_values != nullptr) {
207     for (size_t i = 0; i < vars_vec_size; i++) {
208       varNames.emplace_back(vars_vec[i]);
209       varValues.emplace_back(vars_values[i]);
210     }
211   }
212 
213   return handle->Init(datapath, language, mode, configs, configs_size, &varNames, &varValues,
214                       set_only_non_debug_params != 0);
215 }
216 
TessBaseAPIInit1(TessBaseAPI * handle,const char * datapath,const char * language,TessOcrEngineMode oem,char ** configs,int configs_size)217 int TessBaseAPIInit1(TessBaseAPI *handle, const char *datapath, const char *language,
218                      TessOcrEngineMode oem, char **configs, int configs_size) {
219   return handle->Init(datapath, language, oem, configs, configs_size, nullptr, nullptr, false);
220 }
221 
TessBaseAPIInit2(TessBaseAPI * handle,const char * datapath,const char * language,TessOcrEngineMode oem)222 int TessBaseAPIInit2(TessBaseAPI *handle, const char *datapath, const char *language,
223                      TessOcrEngineMode oem) {
224   return handle->Init(datapath, language, oem);
225 }
226 
TessBaseAPIInit3(TessBaseAPI * handle,const char * datapath,const char * language)227 int TessBaseAPIInit3(TessBaseAPI *handle, const char *datapath, const char *language) {
228   return handle->Init(datapath, language);
229 }
230 
TessBaseAPIGetInitLanguagesAsString(const TessBaseAPI * handle)231 const char *TessBaseAPIGetInitLanguagesAsString(const TessBaseAPI *handle) {
232   return handle->GetInitLanguagesAsString();
233 }
234 
TessBaseAPIGetLoadedLanguagesAsVector(const TessBaseAPI * handle)235 char **TessBaseAPIGetLoadedLanguagesAsVector(const TessBaseAPI *handle) {
236   std::vector<std::string> languages;
237   handle->GetLoadedLanguagesAsVector(&languages);
238   char **arr = new char *[languages.size() + 1];
239   for (auto &language : languages) {
240     arr[&language - &languages[0]] = strdup(language.c_str());
241   }
242   arr[languages.size()] = nullptr;
243   return arr;
244 }
245 
TessBaseAPIGetAvailableLanguagesAsVector(const TessBaseAPI * handle)246 char **TessBaseAPIGetAvailableLanguagesAsVector(const TessBaseAPI *handle) {
247   std::vector<std::string> languages;
248   handle->GetAvailableLanguagesAsVector(&languages);
249   char **arr = new char *[languages.size() + 1];
250   for (auto &language : languages) {
251     arr[&language - &languages[0]] = strdup(language.c_str());
252   }
253   arr[languages.size()] = nullptr;
254   return arr;
255 }
256 
TessBaseAPIInitForAnalysePage(TessBaseAPI * handle)257 void TessBaseAPIInitForAnalysePage(TessBaseAPI *handle) {
258   handle->InitForAnalysePage();
259 }
260 
TessBaseAPIReadConfigFile(TessBaseAPI * handle,const char * filename)261 void TessBaseAPIReadConfigFile(TessBaseAPI *handle, const char *filename) {
262   handle->ReadConfigFile(filename);
263 }
264 
TessBaseAPIReadDebugConfigFile(TessBaseAPI * handle,const char * filename)265 void TessBaseAPIReadDebugConfigFile(TessBaseAPI *handle, const char *filename) {
266   handle->ReadDebugConfigFile(filename);
267 }
268 
TessBaseAPISetPageSegMode(TessBaseAPI * handle,TessPageSegMode mode)269 void TessBaseAPISetPageSegMode(TessBaseAPI *handle, TessPageSegMode mode) {
270   handle->SetPageSegMode(mode);
271 }
272 
TessBaseAPIGetPageSegMode(const TessBaseAPI * handle)273 TessPageSegMode TessBaseAPIGetPageSegMode(const TessBaseAPI *handle) {
274   return handle->GetPageSegMode();
275 }
276 
TessBaseAPIRect(TessBaseAPI * handle,const unsigned char * imagedata,int bytes_per_pixel,int bytes_per_line,int left,int top,int width,int height)277 char *TessBaseAPIRect(TessBaseAPI *handle, const unsigned char *imagedata, int bytes_per_pixel,
278                       int bytes_per_line, int left, int top, int width, int height) {
279   return handle->TesseractRect(imagedata, bytes_per_pixel, bytes_per_line, left, top, width,
280                                height);
281 }
282 
283 #ifndef DISABLED_LEGACY_ENGINE
TessBaseAPIClearAdaptiveClassifier(TessBaseAPI * handle)284 void TessBaseAPIClearAdaptiveClassifier(TessBaseAPI *handle) {
285   handle->ClearAdaptiveClassifier();
286 }
287 #endif
288 
TessBaseAPISetImage(TessBaseAPI * handle,const unsigned char * imagedata,int width,int height,int bytes_per_pixel,int bytes_per_line)289 void TessBaseAPISetImage(TessBaseAPI *handle, const unsigned char *imagedata, int width, int height,
290                          int bytes_per_pixel, int bytes_per_line) {
291   handle->SetImage(imagedata, width, height, bytes_per_pixel, bytes_per_line);
292 }
293 
TessBaseAPISetImage2(TessBaseAPI * handle,struct Pix * pix)294 void TessBaseAPISetImage2(TessBaseAPI *handle, struct Pix *pix) {
295   return handle->SetImage(pix);
296 }
297 
TessBaseAPISetSourceResolution(TessBaseAPI * handle,int ppi)298 void TessBaseAPISetSourceResolution(TessBaseAPI *handle, int ppi) {
299   handle->SetSourceResolution(ppi);
300 }
301 
TessBaseAPISetRectangle(TessBaseAPI * handle,int left,int top,int width,int height)302 void TessBaseAPISetRectangle(TessBaseAPI *handle, int left, int top, int width, int height) {
303   handle->SetRectangle(left, top, width, height);
304 }
305 
TessBaseAPIGetThresholdedImage(TessBaseAPI * handle)306 struct Pix *TessBaseAPIGetThresholdedImage(TessBaseAPI *handle) {
307   return handle->GetThresholdedImage();
308 }
309 
TessBaseAPIClearPersistentCache(TessBaseAPI *)310 void TessBaseAPIClearPersistentCache(TessBaseAPI * /*handle*/) {
311   TessBaseAPI::ClearPersistentCache();
312 }
313 
314 #ifndef DISABLED_LEGACY_ENGINE
315 
TessBaseAPIDetectOrientationScript(TessBaseAPI * handle,int * orient_deg,float * orient_conf,const char ** script_name,float * script_conf)316 BOOL TessBaseAPIDetectOrientationScript(TessBaseAPI *handle, int *orient_deg, float *orient_conf,
317                                         const char **script_name, float *script_conf) {
318   auto success = handle->DetectOrientationScript(orient_deg, orient_conf, script_name, script_conf);
319   return static_cast<BOOL>(success);
320 }
321 
322 #endif
323 
TessBaseAPIGetRegions(TessBaseAPI * handle,struct Pixa ** pixa)324 struct Boxa *TessBaseAPIGetRegions(TessBaseAPI *handle, struct Pixa **pixa) {
325   return handle->GetRegions(pixa);
326 }
327 
TessBaseAPIGetTextlines(TessBaseAPI * handle,struct Pixa ** pixa,int ** blockids)328 struct Boxa *TessBaseAPIGetTextlines(TessBaseAPI *handle, struct Pixa **pixa, int **blockids) {
329   return handle->GetTextlines(pixa, blockids);
330 }
331 
TessBaseAPIGetTextlines1(TessBaseAPI * handle,const BOOL raw_image,const int raw_padding,struct Pixa ** pixa,int ** blockids,int ** paraids)332 struct Boxa *TessBaseAPIGetTextlines1(TessBaseAPI *handle, const BOOL raw_image,
333                                       const int raw_padding, struct Pixa **pixa, int **blockids,
334                                       int **paraids) {
335   return handle->GetTextlines(raw_image != 0, raw_padding, pixa, blockids, paraids);
336 }
337 
TessBaseAPIGetStrips(TessBaseAPI * handle,struct Pixa ** pixa,int ** blockids)338 struct Boxa *TessBaseAPIGetStrips(TessBaseAPI *handle, struct Pixa **pixa, int **blockids) {
339   return handle->GetStrips(pixa, blockids);
340 }
341 
TessBaseAPIGetWords(TessBaseAPI * handle,struct Pixa ** pixa)342 struct Boxa *TessBaseAPIGetWords(TessBaseAPI *handle, struct Pixa **pixa) {
343   return handle->GetWords(pixa);
344 }
345 
TessBaseAPIGetConnectedComponents(TessBaseAPI * handle,struct Pixa ** cc)346 struct Boxa *TessBaseAPIGetConnectedComponents(TessBaseAPI *handle, struct Pixa **cc) {
347   return handle->GetConnectedComponents(cc);
348 }
349 
TessBaseAPIGetComponentImages(TessBaseAPI * handle,TessPageIteratorLevel level,BOOL text_only,struct Pixa ** pixa,int ** blockids)350 struct Boxa *TessBaseAPIGetComponentImages(TessBaseAPI *handle, TessPageIteratorLevel level,
351                                            BOOL text_only, struct Pixa **pixa, int **blockids) {
352   return handle->GetComponentImages(level, static_cast<bool>(text_only), pixa, blockids);
353 }
354 
TessBaseAPIGetComponentImages1(TessBaseAPI * handle,const TessPageIteratorLevel level,const BOOL text_only,const BOOL raw_image,const int raw_padding,struct Pixa ** pixa,int ** blockids,int ** paraids)355 struct Boxa *TessBaseAPIGetComponentImages1(TessBaseAPI *handle, const TessPageIteratorLevel level,
356                                             const BOOL text_only, const BOOL raw_image,
357                                             const int raw_padding, struct Pixa **pixa,
358                                             int **blockids, int **paraids) {
359   return handle->GetComponentImages(level, static_cast<bool>(text_only), raw_image != 0,
360                                     raw_padding, pixa, blockids, paraids);
361 }
362 
TessBaseAPIGetThresholdedImageScaleFactor(const TessBaseAPI * handle)363 int TessBaseAPIGetThresholdedImageScaleFactor(const TessBaseAPI *handle) {
364   return handle->GetThresholdedImageScaleFactor();
365 }
366 
TessBaseAPIAnalyseLayout(TessBaseAPI * handle)367 TessPageIterator *TessBaseAPIAnalyseLayout(TessBaseAPI *handle) {
368   return handle->AnalyseLayout();
369 }
370 
TessBaseAPIRecognize(TessBaseAPI * handle,ETEXT_DESC * monitor)371 int TessBaseAPIRecognize(TessBaseAPI *handle, ETEXT_DESC *monitor) {
372   return handle->Recognize(monitor);
373 }
374 
TessBaseAPIProcessPages(TessBaseAPI * handle,const char * filename,const char * retry_config,int timeout_millisec,TessResultRenderer * renderer)375 BOOL TessBaseAPIProcessPages(TessBaseAPI *handle, const char *filename, const char *retry_config,
376                              int timeout_millisec, TessResultRenderer *renderer) {
377   return static_cast<int>(handle->ProcessPages(filename, retry_config, timeout_millisec, renderer));
378 }
379 
TessBaseAPIProcessPage(TessBaseAPI * handle,struct Pix * pix,int page_index,const char * filename,const char * retry_config,int timeout_millisec,TessResultRenderer * renderer)380 BOOL TessBaseAPIProcessPage(TessBaseAPI *handle, struct Pix *pix, int page_index,
381                             const char *filename, const char *retry_config, int timeout_millisec,
382                             TessResultRenderer *renderer) {
383   return static_cast<int>(
384       handle->ProcessPage(pix, page_index, filename, retry_config, timeout_millisec, renderer));
385 }
386 
TessBaseAPIGetIterator(TessBaseAPI * handle)387 TessResultIterator *TessBaseAPIGetIterator(TessBaseAPI *handle) {
388   return handle->GetIterator();
389 }
390 
TessBaseAPIGetMutableIterator(TessBaseAPI * handle)391 TessMutableIterator *TessBaseAPIGetMutableIterator(TessBaseAPI *handle) {
392   return handle->GetMutableIterator();
393 }
394 
TessBaseAPIGetUTF8Text(TessBaseAPI * handle)395 char *TessBaseAPIGetUTF8Text(TessBaseAPI *handle) {
396   return handle->GetUTF8Text();
397 }
398 
TessBaseAPIGetHOCRText(TessBaseAPI * handle,int page_number)399 char *TessBaseAPIGetHOCRText(TessBaseAPI *handle, int page_number) {
400   return handle->GetHOCRText(nullptr, page_number);
401 }
402 
TessBaseAPIGetAltoText(TessBaseAPI * handle,int page_number)403 char *TessBaseAPIGetAltoText(TessBaseAPI *handle, int page_number) {
404   return handle->GetAltoText(page_number);
405 }
406 
TessBaseAPIGetTsvText(TessBaseAPI * handle,int page_number)407 char *TessBaseAPIGetTsvText(TessBaseAPI *handle, int page_number) {
408   return handle->GetTSVText(page_number);
409 }
410 
TessBaseAPIGetBoxText(TessBaseAPI * handle,int page_number)411 char *TessBaseAPIGetBoxText(TessBaseAPI *handle, int page_number) {
412   return handle->GetBoxText(page_number);
413 }
414 
TessBaseAPIGetWordStrBoxText(TessBaseAPI * handle,int page_number)415 char *TessBaseAPIGetWordStrBoxText(TessBaseAPI *handle, int page_number) {
416   return handle->GetWordStrBoxText(page_number);
417 }
418 
TessBaseAPIGetLSTMBoxText(TessBaseAPI * handle,int page_number)419 char *TessBaseAPIGetLSTMBoxText(TessBaseAPI *handle, int page_number) {
420   return handle->GetLSTMBoxText(page_number);
421 }
422 
TessBaseAPIGetUNLVText(TessBaseAPI * handle)423 char *TessBaseAPIGetUNLVText(TessBaseAPI *handle) {
424   return handle->GetUNLVText();
425 }
426 
TessBaseAPIMeanTextConf(TessBaseAPI * handle)427 int TessBaseAPIMeanTextConf(TessBaseAPI *handle) {
428   return handle->MeanTextConf();
429 }
430 
TessBaseAPIAllWordConfidences(TessBaseAPI * handle)431 int *TessBaseAPIAllWordConfidences(TessBaseAPI *handle) {
432   return handle->AllWordConfidences();
433 }
434 
435 #ifndef DISABLED_LEGACY_ENGINE
TessBaseAPIAdaptToWordStr(TessBaseAPI * handle,TessPageSegMode mode,const char * wordstr)436 BOOL TessBaseAPIAdaptToWordStr(TessBaseAPI *handle, TessPageSegMode mode, const char *wordstr) {
437   return static_cast<int>(handle->AdaptToWordStr(mode, wordstr));
438 }
439 #endif
440 
TessBaseAPIClear(TessBaseAPI * handle)441 void TessBaseAPIClear(TessBaseAPI *handle) {
442   handle->Clear();
443 }
444 
TessBaseAPIEnd(TessBaseAPI * handle)445 void TessBaseAPIEnd(TessBaseAPI *handle) {
446   handle->End();
447 }
448 
TessBaseAPIIsValidWord(TessBaseAPI * handle,const char * word)449 int TessBaseAPIIsValidWord(TessBaseAPI *handle, const char *word) {
450   return handle->IsValidWord(word);
451 }
452 
TessBaseAPIGetTextDirection(TessBaseAPI * handle,int * out_offset,float * out_slope)453 BOOL TessBaseAPIGetTextDirection(TessBaseAPI *handle, int *out_offset, float *out_slope) {
454   return static_cast<int>(handle->GetTextDirection(out_offset, out_slope));
455 }
456 
TessBaseAPIGetUnichar(TessBaseAPI * handle,int unichar_id)457 const char *TessBaseAPIGetUnichar(TessBaseAPI *handle, int unichar_id) {
458   return handle->GetUnichar(unichar_id);
459 }
460 
TessBaseAPISetMinOrientationMargin(TessBaseAPI * handle,double margin)461 void TessBaseAPISetMinOrientationMargin(TessBaseAPI *handle, double margin) {
462   handle->set_min_orientation_margin(margin);
463 }
464 
TessBaseAPINumDawgs(const TessBaseAPI * handle)465 int TessBaseAPINumDawgs(const TessBaseAPI *handle) {
466   return handle->NumDawgs();
467 }
468 
TessBaseAPIOem(const TessBaseAPI * handle)469 TessOcrEngineMode TessBaseAPIOem(const TessBaseAPI *handle) {
470   return handle->oem();
471 }
472 
TessBaseGetBlockTextOrientations(TessBaseAPI * handle,int ** block_orientation,bool ** vertical_writing)473 void TessBaseGetBlockTextOrientations(TessBaseAPI *handle, int **block_orientation,
474                                       bool **vertical_writing) {
475   handle->GetBlockTextOrientations(block_orientation, vertical_writing);
476 }
477 
TessPageIteratorDelete(TessPageIterator * handle)478 void TessPageIteratorDelete(TessPageIterator *handle) {
479   delete handle;
480 }
481 
TessPageIteratorCopy(const TessPageIterator * handle)482 TessPageIterator *TessPageIteratorCopy(const TessPageIterator *handle) {
483   return new TessPageIterator(*handle);
484 }
485 
TessPageIteratorBegin(TessPageIterator * handle)486 void TessPageIteratorBegin(TessPageIterator *handle) {
487   handle->Begin();
488 }
489 
TessPageIteratorNext(TessPageIterator * handle,TessPageIteratorLevel level)490 BOOL TessPageIteratorNext(TessPageIterator *handle, TessPageIteratorLevel level) {
491   return static_cast<int>(handle->Next(level));
492 }
493 
TessPageIteratorIsAtBeginningOf(const TessPageIterator * handle,TessPageIteratorLevel level)494 BOOL TessPageIteratorIsAtBeginningOf(const TessPageIterator *handle, TessPageIteratorLevel level) {
495   return static_cast<int>(handle->IsAtBeginningOf(level));
496 }
497 
TessPageIteratorIsAtFinalElement(const TessPageIterator * handle,TessPageIteratorLevel level,TessPageIteratorLevel element)498 BOOL TessPageIteratorIsAtFinalElement(const TessPageIterator *handle, TessPageIteratorLevel level,
499                                       TessPageIteratorLevel element) {
500   return static_cast<int>(handle->IsAtFinalElement(level, element));
501 }
502 
TessPageIteratorBoundingBox(const TessPageIterator * handle,TessPageIteratorLevel level,int * left,int * top,int * right,int * bottom)503 BOOL TessPageIteratorBoundingBox(const TessPageIterator *handle, TessPageIteratorLevel level,
504                                  int *left, int *top, int *right, int *bottom) {
505   return static_cast<int>(handle->BoundingBox(level, left, top, right, bottom));
506 }
507 
TessPageIteratorBlockType(const TessPageIterator * handle)508 TessPolyBlockType TessPageIteratorBlockType(const TessPageIterator *handle) {
509   return handle->BlockType();
510 }
511 
TessPageIteratorGetBinaryImage(const TessPageIterator * handle,TessPageIteratorLevel level)512 struct Pix *TessPageIteratorGetBinaryImage(const TessPageIterator *handle,
513                                            TessPageIteratorLevel level) {
514   return handle->GetBinaryImage(level);
515 }
516 
TessPageIteratorGetImage(const TessPageIterator * handle,TessPageIteratorLevel level,int padding,struct Pix * original_image,int * left,int * top)517 struct Pix *TessPageIteratorGetImage(const TessPageIterator *handle, TessPageIteratorLevel level,
518                                      int padding, struct Pix *original_image, int *left, int *top) {
519   return handle->GetImage(level, padding, original_image, left, top);
520 }
521 
TessPageIteratorBaseline(const TessPageIterator * handle,TessPageIteratorLevel level,int * x1,int * y1,int * x2,int * y2)522 BOOL TessPageIteratorBaseline(const TessPageIterator *handle, TessPageIteratorLevel level, int *x1,
523                               int *y1, int *x2, int *y2) {
524   return static_cast<int>(handle->Baseline(level, x1, y1, x2, y2));
525 }
526 
TessPageIteratorOrientation(TessPageIterator * handle,TessOrientation * orientation,TessWritingDirection * writing_direction,TessTextlineOrder * textline_order,float * deskew_angle)527 void TessPageIteratorOrientation(TessPageIterator *handle, TessOrientation *orientation,
528                                  TessWritingDirection *writing_direction,
529                                  TessTextlineOrder *textline_order, float *deskew_angle) {
530   handle->Orientation(orientation, writing_direction, textline_order, deskew_angle);
531 }
532 
TessPageIteratorParagraphInfo(TessPageIterator * handle,TessParagraphJustification * justification,BOOL * is_list_item,BOOL * is_crown,int * first_line_indent)533 void TessPageIteratorParagraphInfo(TessPageIterator *handle,
534                                    TessParagraphJustification *justification, BOOL *is_list_item,
535                                    BOOL *is_crown, int *first_line_indent) {
536   bool bool_is_list_item;
537   bool bool_is_crown;
538   handle->ParagraphInfo(justification, &bool_is_list_item, &bool_is_crown, first_line_indent);
539   if (is_list_item != nullptr) {
540     *is_list_item = static_cast<int>(bool_is_list_item);
541   }
542   if (is_crown != nullptr) {
543     *is_crown = static_cast<int>(bool_is_crown);
544   }
545 }
546 
TessResultIteratorDelete(TessResultIterator * handle)547 void TessResultIteratorDelete(TessResultIterator *handle) {
548   delete handle;
549 }
550 
TessResultIteratorCopy(const TessResultIterator * handle)551 TessResultIterator *TessResultIteratorCopy(const TessResultIterator *handle) {
552   return new TessResultIterator(*handle);
553 }
554 
TessResultIteratorGetPageIterator(TessResultIterator * handle)555 TessPageIterator *TessResultIteratorGetPageIterator(TessResultIterator *handle) {
556   return handle;
557 }
558 
TessResultIteratorGetPageIteratorConst(const TessResultIterator * handle)559 const TessPageIterator *TessResultIteratorGetPageIteratorConst(const TessResultIterator *handle) {
560   return handle;
561 }
562 
TessResultIteratorGetChoiceIterator(const TessResultIterator * handle)563 TessChoiceIterator *TessResultIteratorGetChoiceIterator(const TessResultIterator *handle) {
564   return new TessChoiceIterator(*handle);
565 }
566 
TessResultIteratorNext(TessResultIterator * handle,TessPageIteratorLevel level)567 BOOL TessResultIteratorNext(TessResultIterator *handle, TessPageIteratorLevel level) {
568   return static_cast<int>(handle->Next(level));
569 }
570 
TessResultIteratorGetUTF8Text(const TessResultIterator * handle,TessPageIteratorLevel level)571 char *TessResultIteratorGetUTF8Text(const TessResultIterator *handle, TessPageIteratorLevel level) {
572   return handle->GetUTF8Text(level);
573 }
574 
TessResultIteratorConfidence(const TessResultIterator * handle,TessPageIteratorLevel level)575 float TessResultIteratorConfidence(const TessResultIterator *handle, TessPageIteratorLevel level) {
576   return handle->Confidence(level);
577 }
578 
TessResultIteratorWordRecognitionLanguage(const TessResultIterator * handle)579 const char *TessResultIteratorWordRecognitionLanguage(const TessResultIterator *handle) {
580   return handle->WordRecognitionLanguage();
581 }
582 
TessResultIteratorWordFontAttributes(const TessResultIterator * handle,BOOL * is_bold,BOOL * is_italic,BOOL * is_underlined,BOOL * is_monospace,BOOL * is_serif,BOOL * is_smallcaps,int * pointsize,int * font_id)583 const char *TessResultIteratorWordFontAttributes(const TessResultIterator *handle, BOOL *is_bold,
584                                                  BOOL *is_italic, BOOL *is_underlined,
585                                                  BOOL *is_monospace, BOOL *is_serif,
586                                                  BOOL *is_smallcaps, int *pointsize, int *font_id) {
587   bool bool_is_bold;
588   bool bool_is_italic;
589   bool bool_is_underlined;
590   bool bool_is_monospace;
591   bool bool_is_serif;
592   bool bool_is_smallcaps;
593   const char *ret = handle->WordFontAttributes(&bool_is_bold, &bool_is_italic, &bool_is_underlined,
594                                                &bool_is_monospace, &bool_is_serif,
595                                                &bool_is_smallcaps, pointsize, font_id);
596   if (is_bold != nullptr) {
597     *is_bold = static_cast<int>(bool_is_bold);
598   }
599   if (is_italic != nullptr) {
600     *is_italic = static_cast<int>(bool_is_italic);
601   }
602   if (is_underlined != nullptr) {
603     *is_underlined = static_cast<int>(bool_is_underlined);
604   }
605   if (is_monospace != nullptr) {
606     *is_monospace = static_cast<int>(bool_is_monospace);
607   }
608   if (is_serif != nullptr) {
609     *is_serif = static_cast<int>(bool_is_serif);
610   }
611   if (is_smallcaps != nullptr) {
612     *is_smallcaps = static_cast<int>(bool_is_smallcaps);
613   }
614   return ret;
615 }
616 
TessResultIteratorWordIsFromDictionary(const TessResultIterator * handle)617 BOOL TessResultIteratorWordIsFromDictionary(const TessResultIterator *handle) {
618   return static_cast<int>(handle->WordIsFromDictionary());
619 }
620 
TessResultIteratorWordIsNumeric(const TessResultIterator * handle)621 BOOL TessResultIteratorWordIsNumeric(const TessResultIterator *handle) {
622   return static_cast<int>(handle->WordIsNumeric());
623 }
624 
TessResultIteratorSymbolIsSuperscript(const TessResultIterator * handle)625 BOOL TessResultIteratorSymbolIsSuperscript(const TessResultIterator *handle) {
626   return static_cast<int>(handle->SymbolIsSuperscript());
627 }
628 
TessResultIteratorSymbolIsSubscript(const TessResultIterator * handle)629 BOOL TessResultIteratorSymbolIsSubscript(const TessResultIterator *handle) {
630   return static_cast<int>(handle->SymbolIsSubscript());
631 }
632 
TessResultIteratorSymbolIsDropcap(const TessResultIterator * handle)633 BOOL TessResultIteratorSymbolIsDropcap(const TessResultIterator *handle) {
634   return static_cast<int>(handle->SymbolIsDropcap());
635 }
636 
TessChoiceIteratorDelete(TessChoiceIterator * handle)637 void TessChoiceIteratorDelete(TessChoiceIterator *handle) {
638   delete handle;
639 }
640 
TessChoiceIteratorNext(TessChoiceIterator * handle)641 BOOL TessChoiceIteratorNext(TessChoiceIterator *handle) {
642   return static_cast<int>(handle->Next());
643 }
644 
TessChoiceIteratorGetUTF8Text(const TessChoiceIterator * handle)645 const char *TessChoiceIteratorGetUTF8Text(const TessChoiceIterator *handle) {
646   return handle->GetUTF8Text();
647 }
648 
TessChoiceIteratorConfidence(const TessChoiceIterator * handle)649 float TessChoiceIteratorConfidence(const TessChoiceIterator *handle) {
650   return handle->Confidence();
651 }
652 
TessMonitorCreate()653 ETEXT_DESC *TessMonitorCreate() {
654   return new ETEXT_DESC();
655 }
656 
TessMonitorDelete(ETEXT_DESC * monitor)657 void TessMonitorDelete(ETEXT_DESC *monitor) {
658   delete monitor;
659 }
660 
TessMonitorSetCancelFunc(ETEXT_DESC * monitor,TessCancelFunc cancelFunc)661 void TessMonitorSetCancelFunc(ETEXT_DESC *monitor, TessCancelFunc cancelFunc) {
662   monitor->cancel = cancelFunc;
663 }
664 
TessMonitorSetCancelThis(ETEXT_DESC * monitor,void * cancelThis)665 void TessMonitorSetCancelThis(ETEXT_DESC *monitor, void *cancelThis) {
666   monitor->cancel_this = cancelThis;
667 }
668 
TessMonitorGetCancelThis(ETEXT_DESC * monitor)669 void *TessMonitorGetCancelThis(ETEXT_DESC *monitor) {
670   return monitor->cancel_this;
671 }
672 
TessMonitorSetProgressFunc(ETEXT_DESC * monitor,TessProgressFunc progressFunc)673 void TessMonitorSetProgressFunc(ETEXT_DESC *monitor, TessProgressFunc progressFunc) {
674   monitor->progress_callback2 = progressFunc;
675 }
676 
TessMonitorGetProgress(ETEXT_DESC * monitor)677 int TessMonitorGetProgress(ETEXT_DESC *monitor) {
678   return monitor->progress;
679 }
680 
TessMonitorSetDeadlineMSecs(ETEXT_DESC * monitor,int deadline)681 void TessMonitorSetDeadlineMSecs(ETEXT_DESC *monitor, int deadline) {
682   monitor->set_deadline_msecs(deadline);
683 }
684