I'm trying to use a 3rd party library .dll to OCR tif images. I chose Transym http://www.transym.com . In order to access their functions I've written .c wrappers. Currently my sample app GPFs. Can someone help with harbour's vm .c interface?
Here is my self-contained reduced sample code:
- Code: Select all Expand view RUN
//request hb_gt_win //needed for console mode app.
#define TOCRJOBTYPE_TIFFFILE 0 // TOCRJOBINFO.InputFile specifies a tiff file
FUNCTION MAIN()
LOCAL TestTifFile := "sample.tif"
//SetMode(25,80) //25 lines by 80 columns console
IF !FILE( TestTifFile )
Alert( "testfile sample.tif not found" )
return NIL
ENDIF
OCRFromFileUsingTransym( TestTifFile, TOCRJOBTYPE_TIFFFILE )
RETURN NIL
//-----------------------------------------------------------
#pragma BEGINDUMP
// Transym constants below
#define TOCR_OK 0
#define TOCRJOBTYPE_TIFFFILE 0 // TOCRJOBINFO.InputFile specifies a tiff file
#define TOCRJOBTYPE_DIBFILE 1 // TOCRJOBINFO.InputFile specifies a dib (bmp) file
#define TOCRJOBTYPE_DIBCLIPBOARD 2 // clipboard contains a dib (clipboard format CF_DIB)
#define TOCRJOBTYPE_MMFILEHANDLE 3 // TOCRJOBINFO.PageNo specifies a handle to a memory mapped DIB file
#define TOCRCONFIG_DEFAULTJOB -1 // default job number (all new jobs)
#define TOCRCONFIG_DLL_ERRORMODE 0 // set the dll ErrorMode
#define TOCRCONFIG_SRV_ERRORMODE 1 // set the service ErrorMode
#define TOCRCONFIG_SRV_THREADPRIORITY 2 // set the service thread priority
#define TOCRCONFIG_DLL_MUTEXWAIT 3 // set the dll mutex wait timeout (ms)
#define TOCRCONFIG_DLL_EVENTWAIT 4 // set the dll event wait timeout (ms)
#define TOCRCONFIG_SRV_MUTEXWAIT 5 // set the service mutex wait timeout (ms)
#define TOCRCONFIG_LOGFILE 6 // set the log file name
// Setting for JobNo for TOCRSetErrorMode and TOCRGetErrorMode
#define TOCRDEFERRORMODE -1 // set/get the default API error mode (applies
// when there are no jobs and is applied to new jobs)
// Settings for ErrorMode for TOCRSetErrorMode and TOCRGetErrorMode
#define TOCRERRORMODE_NONE 0 // errors unseen (use return status of API calls)
#define TOCRERRORMODE_MSGBOX 1 // errors will bring up a message box
#define TOCRERRORMODE_LOG 2 // errors are sent to a log file
// Values returned by TOCRGetJobStatus JobStatus
#define TOCRJOBSTATUS_ERROR -1 // an error ocurred
#define TOCRJOBSTATUS_BUSY 0 // the job is still processing
#define TOCRJOBSTATUS_DONE 1 // the job completed successfully
#define TOCRJOBSTATUS_IDLE 2 // no job has been specified yet
// Settings for Mode for TOCRGetJobResultsEx
#define TOCRGETRESULTS_NORMAL 0 // return results for TOCRRESULTS
#define TOCRGETRESULTS_EXTENDED 1 // return results for TOCRRESULTSEX
typedef struct tagTOCRProcessOptions
{
long StructId;
short InvertWholePage;
short DeskewOff;
char Orientation;
short NoiseRemoveOff;
short LineRemoveOff;
short DeshadeOff;
short InvertOff;
short SectioningOn;
short MergeBreakOff;
short LineRejectOff;
short CharacterRejectOff;
short LexOff;
short DisableCharacter[256];
} TOCRPROCESSOPTIONS;
typedef struct tagTOCRJobInfo2
{
long StructId;
long JobType;
char *InputFile;
//HANDLE hMMF;
long hMMF;
long PageNo;
TOCRPROCESSOPTIONS ProcessOptions;
} TOCRJOBINFO2;
typedef struct tagTOCRJobInfo
{
long StructId;
long JobType;
char *InputFile;
long PageNo;
TOCRPROCESSOPTIONS ProcessOptions;
} TOCRJOBINFO;
typedef struct tagTOCRRESULTSHEADER
{
long StructId;
long XPixelsPerInch;
long YPixelsPerInch;
long NumItems;
float MeanConfidence;
} TOCRRESULTSHEADER;
typedef struct tagTOCRRESULTSITEM
{
unsigned short StructId;
unsigned short OCRCha;
float Confidence;
unsigned short XPos;
unsigned short YPos;
unsigned short XDim;
unsigned short YDim;
} TOCRRESULTSITEM;
typedef struct tagTOCRRESULTS
{
TOCRRESULTSHEADER Hdr;
TOCRRESULTSITEM Item[1];
} TOCRRESULTS;
typedef struct tagTOCRRESULTSITEMEXALT
{
unsigned short Valid;
unsigned short OCRCha;
float Factor;
} TOCRRESULTSITEMEXALT;
typedef struct tagTOCRRESULTSITEMEX
{
unsigned short StructId;
unsigned short OCRCha;
float Confidence;
unsigned short XPos;
unsigned short YPos;
unsigned short XDim;
unsigned short YDim;
TOCRRESULTSITEMEXALT Alt[5];
} TOCRRESULTSITEMEX;
typedef struct tagTOCRRESULTSEX
{
TOCRRESULTSHEADER Hdr;
TOCRRESULTSITEMEX Item[1];
} TOCRRESULTSEX;
//--------------------------------------------------------
//parameters
// 1. input file with image
// 2. type of file to ocr //should default to TIFF
//returns OCRed text
HB_FUNC( OCRFROMFILEUSINGTRANSYM )
{
TOCRJOBINFO2 JobInfo2;
TOCRRESULTS *Results = 0;
long Status;
long JobNo;
char Msg[8192];
const char * InputFile = hb_parcx( 1 ); //parm 1 is input file
//Sets Transym to show error on a windows dialog box
TOCRSetConfig( TOCRCONFIG_DEFAULTJOB, TOCRCONFIG_DLL_ERRORMODE, TOCRERRORMODE_MSGBOX );
memset( &JobInfo2, 0, sizeof( TOCRJOBINFO2 ) );
JobInfo2.JobType = hb_parni( 2 ) ; //TOCRJOBTYPE_TIFFFILE;
JobInfo2.InputFile = InputFile;
Status = TOCRInitialise( &JobNo );
if ( Status == TOCR_OK ) {
if ( OCRWait( JobNo, JobInfo2 ) ) {
if ( GetResults( JobNo, &Results ) ) {
FormatResults( Results, Msg );
hb_xfree( Results ); //free( Results );
}
}
TOCRShutdown( JobNo );
} /**/
hb_retc( Msg ) ;
}
//--------------------------------------------------------
BOOL OCRWait( long JobNo, TOCRJOBINFO2 JobInfo2 )
{
long Status;
long JobStatus;
long ErrorMode;
char Msg[4096];
Status = TOCRDoJob2( JobNo, &JobInfo2 );
if (Status == TOCR_OK) {
Status = TOCRWaitForJob(JobNo, &JobStatus);
}
if (Status == TOCR_OK && JobStatus == TOCRJOBSTATUS_DONE)
{
return TRUE;
} else {
// If something hass gone wrong display a message
// (Check that the OCR engine hasn't already displayed a message)
TOCRGetConfig(JobNo, TOCRCONFIG_DLL_ERRORMODE, &ErrorMode);
if ( ErrorMode == TOCRERRORMODE_NONE ) {
TOCRGetJobStatusMsg(JobNo, Msg);
//MessageBox( NULL, Msg, "OCRWait", MB_TASKMODAL | MB_TOPMOST | MB_ICONSTOP );
}
return FALSE;
}
} // OCRWait()
//--------------------------------------------------------
// Get the results from TOCR
BOOL getresults(long JobNo, long mode, void **Results)
{
long Status;
long ResultsInf;
char Msg[4096];
Status = TOCRGetJobResultsEx(JobNo, mode, &ResultsInf, 0);
if ( Status != TOCR_OK ) {
//sprintf(Msg, "TOCRGetJobResultsEx failed - %d\n", Status);
//MessageBox(NULL, Msg, "getresults", MB_TASKMODAL | MB_TOPMOST | MB_ICONSTOP);
return FALSE;
}
if ( ResultsInf > 0 ) {
// Allocate memory for results
*Results = ( char * ) hb_xgrab( ResultsInf + 1 );
//memset( &Results, 0, sizeof( ResultsInf ) );
// Retrieve the results
Status = TOCRGetJobResultsEx(JobNo, mode, &ResultsInf, *Results);
if ( Status != TOCR_OK ) {
//sprintf(Msg, "TOCRGetJobResultsEx failed - %d\n", Status);
//MessageBox(NULL, Msg, "getresults", MB_TASKMODAL | MB_TOPMOST | MB_ICONSTOP);
hb_xfree( Results ); //free(*Results);
*Results = 0;
return FALSE;
}
} else {
//MessageBox(NULL, "No results found\n", "getresults", MB_TASKMODAL | MB_TOPMOST | MB_ICONSTOP);
return FALSE ;
}
return TRUE;
} // getresults()
//--------------------------------------------------------
// Get extended results
BOOL GetResults( long JobNo, TOCRRESULTSEX **Results )
{
return getresults( JobNo, TOCRGETRESULTS_EXTENDED, (void **)Results );
} // GetResults()
//--------------------------------------------------------
// Convert extended results to a string
BOOL FormatResults(TOCRRESULTSEX *Results, char *Msg)
{
long ItemNo;
long APos = 0;
BOOL Status = FALSE;
if ( Results->Hdr.NumItems > 0 ) {
for (ItemNo = 0; ItemNo < Results->Hdr.NumItems; ItemNo ++ ) {
if ( Results->Item[ItemNo].OCRCha == '\r' )
Msg[APos] = '\n';
else
Msg[APos] = (char)Results->Item[ItemNo].OCRCha;
APos ++;
}
Msg[APos] = 0;
Status = TRUE;
}
return Status;
} // FormatResults()
#pragma ENDDUMP
If someone here has experience with harbour's vm I will send you the API's documentation and .dll you will need to test. Please write to reinaldo dot crespo at gmail.
Thank you,
Reinaldo.