#include <mil.h>
#define CHAR_IMAGE_FILE M_IMAGE_PATH MIL_TEXT("OcrSemi1292.mim")
#define CHAR_SIZE_X_MIN 22.0
#define CHAR_SIZE_X_MAX 23.0
#define CHAR_SIZE_X_STEP 0.50
#define CHAR_SIZE_Y_MIN 43.0
#define CHAR_SIZE_Y_MAX 44.0
#define CHAR_SIZE_Y_STEP 0.50
#define READ_REGION_POS_X 30L
#define READ_REGION_POS_Y 40L
#define READ_REGION_WIDTH 420L
#define READ_REGION_HEIGHT 70L
#define READ_SCORE_MIN 50.0
#define FONT_FILE_IN M_CONTEXT_PATH MIL_TEXT("SEMI_M12-92.mfo")
#define FONT_FILE_OUT M_TEMP_DIR MIL_TEXT("Semi1292Calibrated.mfo")
#define STRING_LENGTH 13L
#define STRING_CALIBRATION MIL_TEXT("M940902-MXD5")
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay,
MilImage,
MilSubImage,
MilFontSubImage,
MilOverlayImage,
OcrFont,
OcrResult;
MIL_TEXT_CHAR String[STRING_LENGTH];
MIL_DOUBLE Score;
MIL_INT SizeX, SizeY, Type;
MosPrintf(MIL_TEXT("\nOCR MODULE (SEMI font reading):\n"));
MosPrintf(MIL_TEXT("-------------------------------\n\n"));
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
MbufAlloc2d(MilSystem,
MbufDiskInquire(CHAR_IMAGE_FILE, M_SIZE_X, &SizeX),
MbufDiskInquire(CHAR_IMAGE_FILE, M_SIZE_Y, &SizeY)*3/2,
MbufDiskInquire(CHAR_IMAGE_FILE, M_TYPE, &Type),
M_IMAGE+M_PROC+M_DISP,
&MilImage);
MbufClear(MilImage, 0);
MbufLoad(CHAR_IMAGE_FILE, MilImage);
MdispSelect(MilDisplay, MilImage);
MbufChild2d(MilImage, READ_REGION_POS_X, READ_REGION_POS_Y,
READ_REGION_WIDTH, READ_REGION_HEIGHT, &MilSubImage);
MbufChild2d(MilImage, 50, SizeY+10, SizeX-100, (SizeY/3)-10, &MilFontSubImage);
MocrRestoreFont(FONT_FILE_IN, M_RESTORE, MilSystem, &OcrFont);
MocrCopyFont(MilFontSubImage, OcrFont, M_COPY_FROM_FONT+M_ALL_CHAR, M_NULL );
MosPrintf(MIL_TEXT("The SEMI string at the top will be read using the ")
MIL_TEXT("font displayed at the bottom.\n\n"));
MosPrintf(MIL_TEXT("Calibrating SEMI font...\n\n"));
MocrCalibrateFont(MilSubImage, OcrFont, STRING_CALIBRATION,
CHAR_SIZE_X_MIN, CHAR_SIZE_X_MAX, CHAR_SIZE_X_STEP,
CHAR_SIZE_Y_MIN, CHAR_SIZE_Y_MAX, CHAR_SIZE_Y_STEP,
M_DEFAULT);
MocrSetConstraint(OcrFont, 0, M_LETTER, M_NULL);
MocrSetConstraint(OcrFont, 1, M_DIGIT, MIL_TEXT("9"));
MocrSetConstraint(OcrFont, 2, M_DIGIT, M_NULL);
MocrSetConstraint(OcrFont, 3, M_DIGIT, M_NULL);
MocrSetConstraint(OcrFont, 4, M_DIGIT, M_NULL);
MocrSetConstraint(OcrFont, 5, M_DIGIT, M_NULL);
MocrSetConstraint(OcrFont, 6, M_DIGIT, M_NULL);
MocrSetConstraint(OcrFont, 7, M_DEFAULT,MIL_TEXT("-"));
MocrSetConstraint(OcrFont, 8, M_LETTER, MIL_TEXT("M"));
MocrSetConstraint(OcrFont, 9, M_LETTER, MIL_TEXT("X"));
MocrSetConstraint(OcrFont, 10, M_LETTER, MIL_TEXT("ABCDEFGH"));
MocrSetConstraint(OcrFont, 11, M_DIGIT, MIL_TEXT("01234567"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MocrAllocResult(MilSystem, M_DEFAULT, &OcrResult);
MocrReadString(MilSubImage, OcrFont, OcrResult);
MocrGetResult(OcrResult, M_STRING, String);
MocrGetResult(OcrResult, M_STRING_SCORE, &Score);
MosPrintf(MIL_TEXT("\nThe string read is: \"%s\" (score: %.1f%%).\n\n"), String, Score);
MdispControl(MilDisplay, M_OVERLAY, M_ENABLE);
MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT);
MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage);
MgraFont(M_DEFAULT, M_FONT_DEFAULT_LARGE);
MgraColor(M_DEFAULT, M_COLOR_YELLOW);
MgraText(M_DEFAULT, MilOverlayImage, READ_REGION_POS_X+(READ_REGION_WIDTH/4),
READ_REGION_POS_Y+READ_REGION_HEIGHT+50, String);
if (Score > READ_SCORE_MIN)
{
MocrSaveFont(FONT_FILE_OUT, M_SAVE, OcrFont);
MosPrintf(MIL_TEXT("Read successful, calibrated OCR font was saved to disk.\n"));
}
else
{
MosPrintf(MIL_TEXT("Error: Read score too low, calibrated OCR font not saved.\n"));
}
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n\n"));
MosGetch();
MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT);
MocrFree(OcrFont);
MocrFree(OcrResult);
MbufFree(MilSubImage);
MbufFree(MilFontSubImage);
MbufFree(MilImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}