• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 ///////////////////////////////////////////////////////////////////////
2 // File:        tessdll.cpp
3 // Description: Windows dll interface for Tesseract.
4 // Author:      Glen Wernersbach
5 // Created:     Tue May 15 10:30:01 PDT 2007
6 //
7 // (C) Copyright 2007, Jetsoftdev.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 ///////////////////////////////////////////////////////////////////////
19 // tessdll.cpp : Defines the entry point for the DLL application.
20 //
21 
22 #include "stdafx.h"
23 
24 
25 #include "mfcpch.h"
26 #include "applybox.h"
27 #include "control.h"
28 #include "tessvars.h"
29 #include "tessedit.h"
30 #include "pageres.h"
31 #include "imgs.h"
32 #include "varabled.h"
33 #include "tprintf.h"
34 #include "tesseractmain.h"
35 #include "stderr.h"
36 #include "notdll.h"
37 
38 
39 
40 #include "tessdll.h"
41 
42 #ifdef __MSW32__
43 extern ESHM_INFO shm;            /*info on shm */
44 #define TICKS       1000
45 #endif
46 
47 extern BOOL_VARIABLE tessedit_write_ratings;
48 extern BOOL_VARIABLE tessedit_write_output;
49 extern BOOL_VARIABLE tessedit_write_raw_output;
50 extern BOOL_VARIABLE tessedit_write_txt_map;
51 extern BOOL_VARIABLE tessedit_resegment_from_boxes;
52 
53 //unsigned char membuf[sizeof (ETEXT_DESC)+32000L*sizeof (EANYCODE_CHAR)];
54 
DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)55 BOOL APIENTRY DllMain( HANDLE hModule,
56                        DWORD  ul_reason_for_call,
57                        LPVOID lpReserved
58                      )
59 {
60   switch (ul_reason_for_call)
61   {
62     case DLL_PROCESS_ATTACH:
63     case DLL_THREAD_ATTACH:
64     case DLL_THREAD_DETACH:
65     case DLL_PROCESS_DETACH:
66       break;
67   }
68   return TRUE;
69 }
70 
71 
TessDllAPI(const char * lang)72 TessDllAPI::TessDllAPI(const char* lang) {
73   uinT16 oldlang;  //language
74 
75   ocr_open_shm ("0", "0", "0", "0", "0", "0", &oldlang);
76 
77   Init(NULL, lang);
78 
79 
80   if (interactive_mode) {
81     debug_window_on.set_value (TRUE);
82   }
83 
84   tessedit_write_ratings.set_value (TRUE);
85   tessedit_write_output.set_value(FALSE);
86   tessedit_write_raw_output.set_value(FALSE);
87   tessedit_write_txt_map.set_value(FALSE);
88 
89 
90   membuf = (unsigned char *) new BYTE[(sizeof (ETEXT_DESC)+32000L*sizeof (EANYCODE_CHAR))];
91 }
92 
~TessDllAPI()93 TessDllAPI::~TessDllAPI() {
94   EndPage();
95 
96   End();
97 
98   if (membuf) delete []membuf;
99 }
100 
BeginPage(uinT32 xsize,uinT32 ysize,unsigned char * buf)101 int TessDllAPI::BeginPage(uinT32 xsize,uinT32 ysize,unsigned char *buf)
102 {
103     return BeginPage(xsize,ysize,buf,1);
104 }
105 
BeginPage(uinT32 xsize,uinT32 ysize,unsigned char * buf,uinT8 bpp)106 int TessDllAPI::BeginPage(uinT32 xsize,uinT32 ysize,unsigned char *buf,uinT8 bpp) {
107   SetImage(buf, xsize, ysize, bpp/8, (xsize*bpp + 7)/8);
108   return ProcessPagePass1();
109 }
BeginPageUpright(uinT32 xsize,uinT32 ysize,unsigned char * buf)110 int TessDllAPI::BeginPageUpright(uinT32 xsize,uinT32 ysize,unsigned char *buf)
111 {
112 
113   return BeginPageUpright(xsize,ysize,buf,1);
114 }
115 
BeginPageUpright(uinT32 xsize,uinT32 ysize,unsigned char * buf,uinT8 bpp)116 int TessDllAPI::BeginPageUpright(uinT32 xsize,uinT32 ysize,unsigned char *buf, uinT8 bpp) {
117   SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);
118   SetImage(buf, xsize, ysize, bpp/8, (xsize*bpp + 7)/8);
119   return ProcessPagePass1();
120 }
121 
ProcessPagePass1()122 int TessDllAPI::ProcessPagePass1() {
123   if (page_res_ != NULL)
124     ClearResults();
125   if (FindLines() != 0)
126     return -1;
127 
128   page_res_ = new PAGE_RES(block_list_);
129 
130   if (page_res_)
131     tesseract_->recog_all_words(page_res_, global_monitor,0L,1);
132 
133   return (page_res_!=0);
134 }
135 
EndPage()136 void TessDllAPI::EndPage() {
137   ClearResults();
138 }
139 
140 
Recognize_all_Words(void)141 ETEXT_DESC * TessDllAPI::Recognize_all_Words(void) {
142   return Recognize_a_Block(0,0,0,0);
143 }
144 
Recognize_a_Block(uinT32 left,uinT32 right,uinT32 top,uinT32 bottom)145 ETEXT_DESC * TessDllAPI::Recognize_a_Block(uinT32 left,uinT32 right,
146                                            uinT32 top,uinT32 bottom) {
147   TBOX          target_word_box(ICOORD (left+400, top+400), ICOORD (right+400, bottom+400));
148   int           i;
149 
150 
151   shm.shm_size=sizeof (ETEXT_DESC)+32000L*sizeof (EANYCODE_CHAR);
152 
153   memset(membuf,0,shm.shm_size);
154   shm.shm_mem=membuf;
155 
156 
157   global_monitor = ocr_setup_monitor();
158 
159   tesseract_->recog_all_words(page_res_, global_monitor,
160 	                          (right==0 ? 0L : &target_word_box), 2);
161 /* Disabled for now
162   for (i=0;i<global_monitor->count;i++) {
163     global_monitor->text[i].left-=400;
164     global_monitor->text[i].right-=400;
165     global_monitor->text[i].bottom-=400;
166     global_monitor->text[i].top-=400;
167   }
168 */
169 
170   global_monitor = 0L;
171 
172   return ((ETEXT_DESC *) membuf);
173 }
174 
175 TessDllAPI *recognize=0L;
176 char* current_lang = 0L;
177 
178 extern "C"
179 {
180 
TessDllRelease()181 TESSDLL_API void __cdecl TessDllRelease() {
182   if (recognize) delete recognize;
183   recognize=0L;
184 }
185 
TessDllInit(const char * lang)186 TESSDLL_API void  * __cdecl TessDllInit(const char* lang) {
187   if (recognize) TessDllRelease();
188 
189   recognize = new TessDllAPI(lang);
190   if (current_lang != 0L)
191     free(current_lang);
192   current_lang = lang ? strdup(lang) : 0L;
193 
194   return (void*) recognize;
195 }
196 
TessDllBeginPageBPP(uinT32 xsize,uinT32 ysize,unsigned char * buf,uinT8 bpp)197 TESSDLL_API int __cdecl TessDllBeginPageBPP(uinT32 xsize,uinT32 ysize,
198                                          unsigned char *buf, uinT8 bpp) {
199   return TessDllBeginPageLangBPP(xsize, ysize, buf, NULL,bpp);
200 }
201 
TessDllBeginPageLangBPP(uinT32 xsize,uinT32 ysize,unsigned char * buf,const char * lang,uinT8 bpp)202 TESSDLL_API int __cdecl TessDllBeginPageLangBPP(uinT32 xsize, uinT32 ysize,
203                                              unsigned char *buf,
204                                              const char* lang, uinT8 bpp) {
205   if (recognize==0L || (lang != 0L) != (current_lang != 0L) ||
206       lang != 0L && strcmp(lang, current_lang))
207     TessDllInit(lang);
208 
209   return recognize->BeginPage(xsize, ysize, buf,bpp);
210 }
211 
TessDllBeginPageUprightBPP(uinT32 xsize,uinT32 ysize,unsigned char * buf,const char * lang,uinT8 bpp)212 TESSDLL_API int __cdecl TessDllBeginPageUprightBPP(uinT32 xsize, uinT32 ysize,
213                                              unsigned char *buf,
214                                              const char* lang, uinT8 bpp) {
215   if (recognize==0L || (lang != 0L) != (current_lang != 0L) ||
216       lang != 0L && strcmp(lang, current_lang))
217     TessDllInit(lang);
218 
219   return recognize->BeginPageUpright(xsize, ysize, buf,bpp);
220 }
221 
TessDllBeginPage(uinT32 xsize,uinT32 ysize,unsigned char * buf)222 TESSDLL_API int __cdecl TessDllBeginPage(uinT32 xsize,uinT32 ysize,
223                                          unsigned char *buf) {
224   return TessDllBeginPageLangBPP(xsize, ysize, buf, NULL,1);
225 }
226 
TessDllBeginPageLang(uinT32 xsize,uinT32 ysize,unsigned char * buf,const char * lang)227 TESSDLL_API int __cdecl TessDllBeginPageLang(uinT32 xsize, uinT32 ysize,
228                                              unsigned char *buf,
229                                              const char* lang) {
230   if (recognize==0L || (lang != 0L) != (current_lang != 0L) ||
231       lang != 0L && strcmp(lang, current_lang))
232     TessDllInit(lang);
233 
234   return recognize->BeginPage(xsize, ysize, buf,1);
235 }
236 
TessDllBeginPageUpright(uinT32 xsize,uinT32 ysize,unsigned char * buf,const char * lang)237 TESSDLL_API int __cdecl TessDllBeginPageUpright(uinT32 xsize, uinT32 ysize,
238                                              unsigned char *buf,
239                                              const char* lang) {
240   if (recognize==0L || (lang != 0L) != (current_lang != 0L) ||
241       lang != 0L && strcmp(lang, current_lang))
242     TessDllInit(lang);
243 
244   return recognize->BeginPageUpright(xsize, ysize, buf);
245 }
246 
TessDllEndPage(void)247 TESSDLL_API void __cdecl TessDllEndPage(void) {
248   recognize->EndPage();
249 }
250 
TessDllRecognize_a_Block(uinT32 left,uinT32 right,uinT32 top,uinT32 bottom)251 TESSDLL_API ETEXT_DESC * __cdecl TessDllRecognize_a_Block(uinT32 left,
252                                                           uinT32 right,
253                                                           uinT32 top,
254                                                           uinT32 bottom) {
255   return recognize->Recognize_a_Block(left,right,top,bottom);
256 }
257 
258 
TessDllRecognize_all_Words(void)259 TESSDLL_API ETEXT_DESC * __cdecl TessDllRecognize_all_Words(void) {
260   return recognize->Recognize_all_Words();
261 }
262 
263 
264 
265 //deprecated funtions
ReleaseRecognize()266 TESSDLL_API void __cdecl ReleaseRecognize()
267 {
268 
269   if (recognize) delete recognize;recognize=0L;
270 
271 }
272 
273 
274 
275 
InitRecognize()276 TESSDLL_API void  * __cdecl InitRecognize()
277 {
278   if (recognize) ReleaseRecognize();
279 
280   recognize = new TessDllAPI();
281 
282   return (void*) recognize;
283 }
284 
CreateRecognize(uinT32 xsize,uinT32 ysize,unsigned char * buf)285 TESSDLL_API int __cdecl CreateRecognize(uinT32 xsize,uinT32 ysize,unsigned char *buf)
286 {
287   InitRecognize();
288 
289   return recognize->BeginPage(xsize,ysize,buf);
290 
291 }
292 
reconize_a_word(uinT32 left,uinT32 right,uinT32 top,uinT32 bottom)293 TESSDLL_API ETEXT_DESC * __cdecl reconize_a_word(uinT32 left,uinT32 right,uinT32 top,uinT32 bottom)
294 {
295   return recognize->Recognize_a_Block(left,right,top,bottom);
296 }
297 
298 
299 }
300