/*
 *  OcrInterface.cpp
 *  Chipmunk
 *
 *  Created by Trixa on 7/11/09.
 *  Copyright 2009 ABISee. All rights reserved.
 *
 */

#include "..\inc\GoogleOcr.h"
#include "..\inc\OcrInterface.h"
#include <stdlib.h>
#include <string.h>

bool OcrInit(const char *sDataDir, const char *sLang) {
	return TessDllInit(sDataDir, sLang) != NULL;
}

int OcrRecognize(const unsigned char *pImg, unsigned int uImgWidth, unsigned int uImgHeight, OCR_CHAR **ppOcrResult) {
	TessDllBeginPageUprightBPP(uImgWidth, uImgHeight, pImg, 1);
	ETEXT_DESC *pResult = TessDllRecognize_all_Words();
	if(!pResult) {
		return 0;
	}
	int nCount = pResult->count;
	unsigned int uSize = sizeof(OCR_CHAR) * nCount;
	*ppOcrResult = (OCR_CHAR*)malloc(uSize);
	if(!*ppOcrResult) {
		return 0;
	}
	memcpy(*ppOcrResult,  pResult->text, uSize);
	TessDllEndPage();
	return nCount;
}

void OcrTerm() {
	TessDllRelease();
}
