![]() |
wxIScan
|
00001 /*************************************************************** 00002 * Name: wxiscanluascript.cpp 00003 * Purpose: Provides access to a lot of wxIScan functions 00004 * to the Lua language. 00005 * Author: Daniel Nell (daniel.nell@nellresearch.de) 00006 * Created: 2012-02-03 00007 * Copyright: Daniel Nell (www.nellresearch.de) 00008 * License: GPL 00009 **************************************************************/ 00010 00011 00012 // Include precompiled headers. 00013 #include "wx_pch.h" 00014 00015 // wxWidgets headers 00016 #include <wx/filename.h> 00017 00018 // Private headers 00019 #include "wxiscanframe.h" 00020 #include "wxiscanglobals.h" 00021 #if __WXLUASCRIPT__ 00022 # include "wxiscanluascript.h" 00023 #endif // __WXLUASCRIPT__ 00024 00025 00026 #if __WXLUASCRIPT__ 00027 ///////////////////////////////////////////////////////////////////////////// 00028 // Class wxIScanLuaScript 00029 // 00030 // Standard constructor. 00031 // 00032 wxIScanLuaScript::wxIScanLuaScript( const wxString& strScript, wxEvtHandler *poParent, const wxString& strDomain, bool *pbParentState ) 00033 : wxLuaScript( strScript, poParent, strDomain, pbParentState ) 00034 { 00035 #if __WXLUASCRIPT_DYNAMIC__ 00036 // Check if the dynamic library is initialized correctly. 00037 if( !IsOk() ) 00038 { 00039 #if __DEBUG__ 00040 // Log waring message in DEBUG mode. 00041 wxLogWarning( wxString( wxT( "wxIScanLuaScript::wxIScanLuaScript -- " ) ) 00042 + _( "Base class was not correctly initialized." ) ); 00043 #endif // __DEBUG__ 00044 00045 // Abort further initialization. 00046 return; 00047 } 00048 #endif // __WXLUASCRIPT_DYNAMIC__ 00049 00050 // Register "OCR" as Lua function. 00051 RegisterFunction( wxT( "OCR" ), OCR ); 00052 00053 // Register "OpenImage" (and its alias "LoadImage") as Lua function. 00054 RegisterFunction( wxT( "OpenImage" ), OpenImage ); 00055 RegisterFunction( wxT( "LoadImage" ), OpenImage ); 00056 00057 // Register "SaveImage" as Lua function. 00058 RegisterFunction( wxT( "SaveImage" ), SaveImage ); 00059 00060 // Register "CopyText2Clipboard" as Lua function. 00061 RegisterFunction( wxT( "CopyText2Clipboard" ), CopyText2Clipboard ); 00062 00063 // Register "Image2Mono" as Lua function. 00064 RegisterFunction( wxT( "Image2Mono" ), Image2Mono ); 00065 00066 // Register "Image2Grey" as Lua function. 00067 RegisterFunction( wxT( "Image2Grey" ), Image2Grey ); 00068 00069 // Register "CropImage" as Lua function. 00070 RegisterFunction( wxT( "CropImage" ), CropImage ); 00071 00072 // Register "EnhenceColours" as Lua function. 00073 RegisterFunction( wxT( "EnhenceColours" ), EnhenceColours ); 00074 00075 // Register "EnhanceOpticalContrast" as Lua function. 00076 RegisterFunction( wxT( "EnhenceOpticalContrast" ), EnhenceOpticalContrast ); 00077 00078 // Register "RotateImage" as Lua function. 00079 RegisterFunction( wxT( "RotateImage" ), RotateImage ); 00080 00081 // Register "RotateImage90" as Lua function. 00082 RegisterFunction( wxT( "RotateImage90" ), RotateImage90 ); 00083 00084 // Register "Execute" as Lua function. 00085 RegisterFunction( wxT( "Execute" ), Execute ); 00086 00087 // Register "ResetFileName" as Lua function. 00088 RegisterFunction( wxT( "ResetFileName" ), ResetFileName ); 00089 } 00090 00091 // Do OCR on the loaded/scanned image and return the text (to Lua). 00092 // 00093 int wxIScanLuaScript::OCR( lua_State* L ) 00094 { 00095 int nNoOfArgs= lua_gettop( L ); 00096 00097 // Check the number of argument(s) and the type(s). 00098 if( ( nNoOfArgs != 0 ) 00099 && ( ( nNoOfArgs != 1 ) || !lua_isboolean( L, 1 ) ) ) 00100 { 00101 // Signal error. 00102 lua_pushstring( L, "Incorrect argument or incorrect number of arguments." ); 00103 lua_error( L ); 00104 } 00105 00106 // Get the flag if hOCR processing should be used. 00107 bool bUseHocr= ( nNoOfArgs == 1 ) ? lua_toboolean( L, 1 ) : false; 00108 00109 // Get the this pointer to the current wxLuaScript object. 00110 wxIScanLuaScript *poThis= (wxIScanLuaScript *)GetThis( L ); 00111 00112 // Get the pointer to the event handler (the frame window). 00113 wxIScanFrame *poParent= (wxIScanFrame *)poThis->m_poParent; 00114 00115 // Save image in a temporary file. 00116 wxString strTempFileName= wxFileName::CreateTempFileName( wxT( "" ) ); 00117 00118 poParent->GetImageSelection().SaveFile( strTempFileName, poParent->m_strBitmapMimeType ); 00119 00120 // Do OCR (and get the file name of the generated temporary text/hOCR file). 00121 wxString strOcrTextFileName= poParent->OCR( strTempFileName, bUseHocr ); 00122 00123 // Remove the temporary JPEG file. 00124 ::wxRemoveFile( strTempFileName ); 00125 00126 // Return the text. 00127 if( !strOcrTextFileName.IsEmpty() ) 00128 { 00129 // Get the text from the temporary text file, ... 00130 wxString strText= ::GetTextFromFile( strOcrTextFileName ); 00131 00132 // ... return it, and ... 00133 lua_pushstring( L, strText.char_str( wxConvISO8859_1 ) ); 00134 00135 // ... remove the temporary text file. 00136 ::wxRemoveFile( strOcrTextFileName ); 00137 } 00138 else 00139 { 00140 // Signal error. 00141 lua_pushstring( L, "OCR failed." ); 00142 lua_error( L ); 00143 } 00144 00145 // Return the number of (Lua) results. 00146 return 1; 00147 } 00148 00149 // Open an image file in Lua using a given name. 00150 // 00151 int wxIScanLuaScript::OpenImage( lua_State* L ) 00152 { 00153 // Check the number of argument(s) and the type(s). 00154 int nNoOfArgs= lua_gettop( L ); 00155 00156 if( !( ( nNoOfArgs == 1 ) && lua_isstring( L, 1 ) ) 00157 && !( ( nNoOfArgs == 2 ) && lua_isstring( L, 1 ) && lua_isboolean( L, 2 ) ) 00158 ) 00159 { 00160 // Signal error. 00161 lua_pushstring( L, "Incorrect argument or incorrect number of arguments." ); 00162 lua_error( L ); 00163 } 00164 00165 // Get the filename. 00166 wxString strFileName( (const char *)lua_tostring( L, 1 ), wxConvISO8859_1 ); 00167 00168 // Get ... 00169 bool bSetFileName= ( nNoOfArgs == 2 ) ? lua_toboolean( L, 2 ) : false; 00170 00171 // Get the this pointer to the current wxLuaScript object. 00172 wxIScanLuaScript *poThis= (wxIScanLuaScript *)GetThis( L ); 00173 00174 // Get the pointer to the event handler (the frame window). 00175 wxIScanFrame *poParent= (wxIScanFrame *)poThis->m_poParent; 00176 00177 // Open the image and return the status to Lua. 00178 lua_pushboolean( L, poParent->OpenImage( strFileName, bSetFileName ) ); 00179 00180 // Return the number of (Lua) results. 00181 return 1; 00182 } 00183 00184 // Save an image file in Lua using a given name. 00185 // 00186 int wxIScanLuaScript::SaveImage( lua_State* L ) 00187 { 00188 // Check the number of argument(s) and the type(s). 00189 int nNoOfArgs= lua_gettop( L ); 00190 00191 if( !( ( nNoOfArgs == 1 ) && lua_isstring( L, 1 ) ) 00192 && !( ( nNoOfArgs == 2 ) && lua_isstring( L, 1 ) && lua_isstring( L, 2 ) ) 00193 ) 00194 { 00195 // Signal error. 00196 lua_pushstring( L, "Incorrect argument or incorrect number of arguments." ); 00197 lua_error( L ); 00198 } 00199 00200 // Get the this pointer to the current wxLuaScript object. 00201 wxIScanLuaScript *poThis= (wxIScanLuaScript *)GetThis( L ); 00202 00203 // Get the pointer to the event handler (the frame window). 00204 wxIScanFrame *poParent= (wxIScanFrame *)poThis->m_poParent; 00205 00206 // Get the filename. 00207 wxString strFileName( (const char *)lua_tostring( L, 1 ), wxConvISO8859_1 ); 00208 00209 // Save the image and return the status to Lua. 00210 if( nNoOfArgs == 2 ) 00211 { 00212 wxString strMimeType( (const char *)lua_tostring( L, 2 ), wxConvISO8859_1 ); 00213 00214 lua_pushboolean( L, poParent->GetImageSelection().SaveFile( strFileName, strMimeType ) ); 00215 } 00216 else 00217 { 00218 lua_pushboolean( L, poParent->GetImageSelection().SaveFile( strFileName ) ); 00219 } 00220 00221 // Return the number of (Lua) results. 00222 return 1; 00223 } 00224 00225 // Copy a given text to the clipboard in Lua. 00226 // 00227 int wxIScanLuaScript::CopyText2Clipboard( lua_State* L ) 00228 { 00229 // Check the number of argument(s) and the type(s). 00230 if( ( lua_gettop( L ) != 1 ) || !lua_isstring( L, 1 ) ) 00231 { 00232 // Signal error. 00233 lua_pushstring( L, "Incorrect argument or incorrect number of arguments." ); 00234 lua_error( L ); 00235 } 00236 00237 // Get the text. 00238 wxString strText( (const char *)lua_tostring( L, 1 ), wxConvISO8859_1 ); 00239 00240 // Copy the text to the clipboard and return the status to Lua. 00241 lua_pushboolean( L, ::CopyText2Clipboard( strText ) ); 00242 00243 // Return the number of (Lua) results. 00244 return 1; 00245 } 00246 00247 // Replace the program's image by its monochrome version. 00248 // 00249 int wxIScanLuaScript::Image2Mono( lua_State* L ) 00250 { 00251 // Check the number of argument(s) and the type(s). 00252 int nNoOfArgs= lua_gettop( L ); 00253 00254 if( !( nNoOfArgs == 0 ) 00255 && !( ( nNoOfArgs == 1 ) && lua_isnumber( L, 1 ) ) ) 00256 { 00257 // Signal error. 00258 lua_pushstring( L, "Incorrect argument or incorrect number of arguments." ); 00259 lua_error( L ); 00260 } 00261 00262 // Get the this pointer to the current wxLuaScript object. 00263 wxIScanLuaScript *poThis= (wxIScanLuaScript *)GetThis( L ); 00264 00265 // Get the pointer to the event handler (the frame window). 00266 wxIScanFrame *poParent= (wxIScanFrame *)poThis->m_poParent; 00267 00268 // Convert the image to a monochrome image. 00269 if( nNoOfArgs == 1 ) 00270 { 00271 poParent->m_oImage= ::Image2Mono( poParent->m_oImage, lua_tointeger( L, 1 ) ); 00272 } 00273 else 00274 { 00275 poParent->m_oImage= ::Image2Mono( poParent->m_oImage ); 00276 } 00277 poParent->UpdateBitmap(); 00278 00279 // Return the number of (Lua) results. 00280 return 0; 00281 } 00282 00283 // Replace the program's image by its greyscale version. 00284 // 00285 int wxIScanLuaScript::Image2Grey( lua_State* L ) 00286 { 00287 // Check the number of argument(s) and the type(s). 00288 int nNoOfArgs= lua_gettop( L ); 00289 00290 if( !( nNoOfArgs == 0 ) ) 00291 { 00292 // Signal error. 00293 lua_pushstring( L, "Incorrect number of arguments." ); 00294 lua_error( L ); 00295 } 00296 00297 // Get the this pointer to the current wxLuaScript object. 00298 wxIScanLuaScript *poThis= (wxIScanLuaScript *)GetThis( L ); 00299 00300 // Get the pointer to the event handler (the frame window). 00301 wxIScanFrame *poParent= (wxIScanFrame *)poThis->m_poParent; 00302 00303 // Convert the image to a monochrome image. 00304 poParent->m_oImage= ::Image2Grey( poParent->m_oImage ); 00305 poParent->UpdateBitmap(); 00306 00307 // Return the number of (Lua) results. 00308 return 0; 00309 } 00310 00311 // Replace the program's image by its cropped version. 00312 // 00313 int wxIScanLuaScript::CropImage( lua_State* L ) 00314 { 00315 // Check the number of argument(s) and the type(s). 00316 int nNoOfArgs= lua_gettop( L ); 00317 00318 if( !( nNoOfArgs == 4 ) 00319 || !lua_isnumber( L, 1 ) 00320 || !lua_isnumber( L, 2 ) 00321 || !lua_isnumber( L, 3 ) 00322 || !lua_isnumber( L, 4 ) 00323 ) 00324 { 00325 // Signal error. 00326 lua_pushstring( L, "Incorrect argument or incorrect number of arguments." ); 00327 lua_error( L ); 00328 } 00329 00330 // Get the this pointer to the current wxLuaScript object. 00331 wxIScanLuaScript *poThis= (wxIScanLuaScript *)GetThis( L ); 00332 00333 // Get the pointer to the event handler (the frame window). 00334 wxIScanFrame *poParent= (wxIScanFrame *)poThis->m_poParent; 00335 00336 // Crop the image. 00337 poParent->m_oImage= ::CropImage( poParent->m_oImage, 00338 lua_tointeger( L, 1 ), 00339 lua_tointeger( L, 2 ), 00340 lua_tointeger( L, 3 ), 00341 lua_tointeger( L, 4 ) ); 00342 poParent->UpdateBitmap(); 00343 00344 // Return the number of (Lua) results. 00345 return 0; 00346 } 00347 00348 // Replace the program's image by a new version that is colour enhenced. 00349 // 00350 int wxIScanLuaScript::EnhenceColours( lua_State* L ) 00351 { 00352 int anParam[]= { 0, 0, 100, 0, 0, 0 }; 00353 int nNoOfArgs= lua_gettop( L ); 00354 00355 // Check the number of argument(s), and ... 00356 if( ( nNoOfArgs < 0 ) || ( nNoOfArgs > 6 ) ) 00357 { 00358 // Signal error. 00359 lua_pushstring( L, "Incorrect number of arguments." ); 00360 lua_error( L ); 00361 } 00362 00363 // ... the type(s), and ... 00364 for( int i= 0; i < nNoOfArgs; i++ ) 00365 { 00366 // ... get the value from Lua; 00367 if( lua_isnumber( L, i + 1 ) ) 00368 { 00369 anParam[i]= lua_tointeger( L, i + 1 ); 00370 } 00371 else 00372 { 00373 // Signal error. 00374 lua_pushstring( L, "Incorrect argument type." ); 00375 lua_error( L ); 00376 00377 } 00378 } 00379 00380 // Get the this pointer to the current wxLuaScript object. 00381 wxIScanLuaScript *poThis= (wxIScanLuaScript *)GetThis( L ); 00382 00383 // Get the pointer to the event handler (the frame window). 00384 wxIScanFrame *poParent= (wxIScanFrame *)poThis->m_poParent; 00385 00386 // Replace the program's image by the colour enhenced one. 00387 poParent->m_oImage= ::EnhenceColours( poParent->m_oImage, 00388 anParam[0], anParam[1], anParam[2], 00389 anParam[3], anParam[4], anParam[5] ); 00390 poParent->UpdateBitmap(); 00391 00392 // Return the number of (Lua) results. 00393 return 0; 00394 } 00395 00396 // Replace white colour by yellow. 00397 // 00398 int wxIScanLuaScript::EnhenceOpticalContrast( lua_State* L ) 00399 { 00400 // Check the number of argument(s) and the type(s). 00401 if( lua_gettop( L ) != 0 ) 00402 { 00403 // Signal error. 00404 lua_pushstring( L, "Incorrect number of arguments." ); 00405 lua_error( L ); 00406 } 00407 00408 // Get the this pointer to the current wxLuaScript object. 00409 wxIScanLuaScript *poThis= (wxIScanLuaScript *)GetThis( L ); 00410 00411 // Get the pointer to the event handler (the frame window). 00412 wxIScanFrame *poParent= (wxIScanFrame *)poThis->m_poParent; 00413 00414 // Replace white colour by yellow. 00415 poParent->EnhenceOpticalContrast(); 00416 00417 // Return the number of (Lua) results. 00418 return 0; 00419 } 00420 00421 // (Fine) Rotate an image by a given angle. 00422 // 00423 int wxIScanLuaScript::RotateImage( lua_State* L ) 00424 { 00425 // Check the number of argument(s) and the type(s). 00426 if( ( lua_gettop( L ) != 1 ) || !lua_isnumber( L, 1 ) ) 00427 { 00428 // Signal error. 00429 lua_pushstring( L, "Incorrect argument or incorrect number of arguments." ); 00430 lua_error( L ); 00431 } 00432 00433 // Get the this pointer to the current wxLuaScript object. 00434 wxIScanLuaScript *poThis= (wxIScanLuaScript *)GetThis( L ); 00435 00436 // Get the pointer to the event handler (the frame window). 00437 wxIScanFrame *poParent= (wxIScanFrame *)poThis->m_poParent; 00438 00439 // Rotate the image an replace it by the output. 00440 poParent->m_oImage= ::RotateImage( poParent->m_oImage, lua_tonumber( L, 1 ) ); 00441 poParent->UpdateBitmap(); 00442 00443 // Return the number of (Lua) results. 00444 return 0; 00445 } 00446 00447 // Rotate an image by a given angle by 90 degrees.. 00448 // 00449 int wxIScanLuaScript::RotateImage90( lua_State* L ) 00450 { 00451 // Check the number of argument(s) and the type(s). 00452 int nNoOfArgs= lua_gettop( L ); 00453 00454 if( !( nNoOfArgs == 0 ) 00455 && !( ( nNoOfArgs == 1 ) && lua_isboolean( L, 1 ) ) ) 00456 { 00457 // Signal error. 00458 lua_pushstring( L, "Incorrect argument or incorrect number of arguments." ); 00459 lua_error( L ); 00460 } 00461 00462 // Get the this pointer to the current wxLuaScript object. 00463 wxIScanLuaScript *poThis= (wxIScanLuaScript *)GetThis( L ); 00464 00465 // Get the pointer to the event handler (the frame window). 00466 wxIScanFrame *poParent= (wxIScanFrame *)poThis->m_poParent; 00467 00468 // Rotate the image and replace it by the output. 00469 poParent->m_oImage= poParent->m_oImage.Rotate90( ( nNoOfArgs == 1 ) ? lua_toboolean( L, 1 ) : true ); 00470 poParent->UpdateBitmap(); 00471 00472 // Return the number of (Lua) results. 00473 return 0; 00474 } 00475 00476 // Resize an image to the given size. 00477 // 00478 int wxIScanLuaScript::ResizeImage( lua_State* L ) 00479 { 00480 // Check the number of argument(s) and the type(s). 00481 if( ( lua_gettop( L ) != 2 ) || !lua_isnumber( L, 1 ) || !lua_isnumber( L, 2 ) ) 00482 { 00483 // Signal error. 00484 lua_pushstring( L, "Incorrect argument or incorrect number of arguments." ); 00485 lua_error( L ); 00486 } 00487 00488 // Get the this pointer to the current wxLuaScript object. 00489 wxIScanLuaScript *poThis= (wxIScanLuaScript *)GetThis( L ); 00490 00491 // Get the pointer to the event handler (the frame window). 00492 wxIScanFrame *poParent= (wxIScanFrame *)poThis->m_poParent; 00493 00494 // Rosize the image an replace it by the output. 00495 poParent->m_oImage= ::ResizeImage( poParent->m_oImage, 00496 lua_tonumber( L, 1 ), lua_tointeger( L, 2 ) ); 00497 poParent->UpdateBitmap(); 00498 00499 // Return the number of (Lua) results. 00500 return 0; 00501 } 00502 00503 // Fork an external process. 00504 // 00505 int wxIScanLuaScript::Execute( lua_State* L ) 00506 { 00507 // Check the number of argument(s) and the type(s). 00508 int nNoOfArgs= lua_gettop( L ); 00509 00510 if( !( ( nNoOfArgs == 1 ) && lua_isstring( L, 1 ) ) 00511 && !( ( nNoOfArgs == 2 ) && lua_isstring( L, 1 ) && lua_isboolean( L, 2 ) ) 00512 && !( ( nNoOfArgs == 3 ) && lua_isstring( L, 1 ) && lua_isboolean( L, 2 ) && lua_isstring( L, 3 ) ) 00513 ) 00514 { 00515 // Signal error. 00516 lua_pushstring( L, "Incorrect argument or incorrect number of arguments." ); 00517 lua_error( L ); 00518 } 00519 00520 // Get the this pointer to the current wxLuaScript object. 00521 wxIScanLuaScript *poThis= (wxIScanLuaScript *)GetThis( L ); 00522 00523 // Get the pointer to the event handler (the frame window). 00524 wxIScanFrame *poParent= (wxIScanFrame *)poThis->m_poParent; 00525 00526 // Execute the command. 00527 bool bRetC= false; 00528 00529 switch( nNoOfArgs ) 00530 { 00531 case 1: bRetC= poParent->Execute( 00532 wxString( (const char *)lua_tostring( L, 1 ), wxConvISO8859_1 ) 00533 ); 00534 break; 00535 case 2: bRetC= poParent->Execute( 00536 wxString( (const char *)lua_tostring( L, 1 ), wxConvISO8859_1 ), 00537 lua_toboolean( L, 2 ) 00538 ); 00539 break; 00540 case 3: bRetC= poParent->Execute( 00541 wxString( (const char *)lua_tostring( L, 1 ), wxConvISO8859_1 ), 00542 lua_toboolean( L, 2 ), 00543 wxString( (const char *)lua_tostring( L, 3 ), wxConvISO8859_1 ) 00544 ); 00545 break; 00546 } 00547 00548 // Return a boolean that flags success (true) or failure (false). 00549 lua_pushboolean( L, bRetC ); 00550 00551 // Return the number of (Lua) results. 00552 return 1; 00553 } 00554 00555 // Set the filename of the image to "unknown.XXX". 00556 // 00557 int wxIScanLuaScript::ResetFileName( lua_State* L ) 00558 { 00559 // Check the number of argument(s) and the type(s). 00560 if( lua_gettop( L ) != 0 ) 00561 { 00562 // Signal error. 00563 lua_pushstring( L, "Incorrect number of arguments." ); 00564 lua_error( L ); 00565 } 00566 00567 // Get the this pointer to the current wxLuaScript object. 00568 wxIScanLuaScript *poThis= (wxIScanLuaScript *)GetThis( L ); 00569 00570 // Get the pointer to the event handler (the frame window). 00571 wxIScanFrame *poParent= (wxIScanFrame *)poThis->m_poParent; 00572 00573 // Set the filename of the image to "unknown.XXX". 00574 poParent->ResetFileName(); 00575 00576 // Return the number of (Lua) results. 00577 return 0; 00578 } 00579 #endif // __WXLUASCRIPT__