#include "StdAfx.h" #include "OcrNode.h" #include "OcrScreen.h" #include "common_funcs.h" #include "common_api.h" OcrNode::OcrNode(void) { m_TotalPixelsCount = 0; } OcrNode::~OcrNode(void) { ULONG i = 0; OcrLine *pNode; for(i = 0;i < m_lists.size();i++) { pNode = m_lists[i]; delete pNode; } m_lists.clear(); } BOOL OcrNode::GetMatchRate(ULONG64 sim) { ULONG64 matched = 0; ULONG i = 0; OcrLine *pNode; for(i = 0;i < m_lists.size();i++) { pNode = m_lists[i]; matched += pNode->m_MatchCount; } if((matched*100) >= (m_TotalPixelsCount*sim)) { return TRUE; } return FALSE; float retf = ((float)matched)/((float)m_TotalPixelsCount); return (ULONG)(retf*100); } void OcrNode::GetFitAreaX(COLORPOINT &topleft,COLORPOINT &topright,COLORPOINT &bottomleft,COLORPOINT &bottomright) { m_lists[0]->GetLineKey(topleft,topright); m_lists[m_lists.size() - 1]->GetLineKey(bottomleft,bottomright); } void OcrNode::GetFitArea(COLORPOINT &top1,COLORPOINT &left1,COLORPOINT &bottom1,COLORPOINT &right1) { COLORPOINT linex[2]; memset(&top1,-1,sizeof(COLORPOINT)); memset(&bottom1,-1,sizeof(COLORPOINT)); memset(&left1,-1,sizeof(COLORPOINT)); memset(&right1,-1,sizeof(COLORPOINT)); ULONG i = 0; for(i = 0;i < m_lists.size();i++) { m_lists[i]->GetLineKey(linex[0],linex[1]); //top if(top1.pt.y < 0) { top1 = linex[0]; } else { if(top1.pt.y > linex[0].pt.y) { top1 = linex[0]; } } //bottom if(bottom1.pt.y < 0) { bottom1 = linex[0]; } else { if(bottom1.pt.y < linex[0].pt.y) { bottom1 = linex[0]; } } //left if(left1.pt.x < 0) { left1 = linex[0]; } else { if(left1.pt.x > linex[0].pt.x) { left1 = linex[0]; } } //right if(right1.pt.x < 0) { right1 = linex[1]; } else { if(right1.pt.x < linex[1].pt.x) { right1 = linex[1]; } } } } BOOL OcrNode::InitialLoadOcrFromFile(const TCHAR *pszFulName) { //key point COLORPOINT top1,left1,bottom1,right1; GetFitArea(top1,left1,bottom1,right1); //reFix start point for(DWORD i = 0;i < m_lists.size();i++) { m_lists[i]->m_pt.x -= left1.pt.x; m_lists[i]->m_pt.y -= top1.pt.y; } GetFitArea(top1,left1,bottom1,right1); m_size.cx = right1.pt.x - left1.pt.x + 1; m_size.cy = bottom1.pt.y - top1.pt.y + 1; //reGet FitArea GetFitAreaX(top1,left1,bottom1,right1); m_KeyPoints[0] = top1; m_KeyPoints[1] = bottom1; m_KeyPoints[2] = left1; m_KeyPoints[3] = right1; //file name //CFile file; //file.Open(pszFulName,CFile::modeRead); //CString temp = file.GetFileTitle(); //temp.Replace(L".bmp",L""); string temp = pszFulName; m_NodeName = GetFileTitle(temp); //file.Close(); InitialSundayLine(); return TRUE; } void OcrNode::InitialSundayLine() { m_SundayIndex = 0; INT length = 0; //search longest line for(DWORD i = 0;i < m_lists.size();i++) { if(m_lists[i]->m_LimitedSize.cx > length) { length = m_lists[i]->m_LimitedSize.cx; m_SundayIndex = i; } } //do the map m_lists[m_SundayIndex]->MakeSundayLine(); } BOOL OcrNode::LoadOcrFromFile(const TCHAR *pszFulName) { OCRCOLOR mask_color; //load bitmap DWORD width,height; PBYTE pBits = LoadBitMapFromFile(pszFulName,width,height); if(pBits == NULL) { return FALSE; } m_size.cx = width; m_size.cy = height; //get mask color OcrScreen screen(pBits,m_size); delete []pBits; COLORPOINT cpt; //top left cpt.pt.x = 0; cpt.pt.y = 0; screen.GetPointColor(cpt); mask_color = cpt.color; //top right cpt.pt.x = width - 1; screen.GetPointColor(cpt); if(mask_color != cpt.color) { return FALSE; } //bottom right cpt.pt.y = height - 1; screen.GetPointColor(cpt); if(mask_color != cpt.color) { return FALSE; } //bottom left cpt.pt.x = 0; screen.GetPointColor(cpt); if(mask_color != cpt.color) { return FALSE; } POINT linestartpt; pBits = new BYTE[8192*sizeof(OCRCOLOR)];//a line //cut it to each single line for(DWORD i = 0;i < height;i++) { DWORD copyedline = 0; for(DWORD j = 0;j < width;j++) { cpt.pt.x = j; cpt.pt.y = i; screen.GetPointColor(cpt); if(cpt.color != mask_color) { //got a line's start pt if(copyedline == 0) { linestartpt = cpt.pt; } memcpy(&pBits[copyedline*sizeof(OCRCOLOR)] , &(cpt.color),sizeof(OCRCOLOR)); ++copyedline; } else { if(copyedline > 0) { SIZE linesize; linesize.cy = 1; linesize.cx = copyedline; //a line detected OcrLine *pLine = new OcrLine(pBits,linestartpt,linesize); m_lists.push_back(pLine); m_TotalPixelsCount += copyedline; copyedline = 0; } } } } delete []pBits; InitialLoadOcrFromFile(pszFulName); return TRUE; } void OcrNode::ClearRecord() { ULONG i = 0; OcrLine *pNode; for(i = 0;i < m_lists.size();i++) { pNode = m_lists[i]; pNode->ClearRecord(); } } BOOL OcrNode::CheckMainKeyMatch(OcrScreen &fullPic, POINT &start1, OCRCOLOR color, ULONG sim) { if(sim != 100) { return TRUE; } COLORPOINT temp[4]; memcpy(temp,m_KeyPoints,sizeof(COLORPOINT)*4); for(int i = 0;i < 4;i++) { temp[i].pt.x += start1.x; temp[i].pt.y += start1.y; fullPic.GetPointColor(temp[i]); if(CheckTwoColors(temp[i].color,m_KeyPoints[i].color,color) == FALSE) { return FALSE; } } return TRUE; } BOOL OcrNode::CheckLineKeyMatch(OcrScreen &fullPic, POINT &start1, OCRCOLOR color, ULONG sim) { if(sim != 100) { return TRUE; } for(ULONG i = 0;i < m_lists.size();i++) { if(m_lists[i]->CheckKeyMatch(fullPic,start1,color) == FALSE) { return FALSE; } } return TRUE; } BOOL OcrNode::CheckLineMatch(OcrScreen &fullPic, POINT &start1, OCRCOLOR color, ULONG sim) { for(ULONG i = 0;i < m_lists.size();i++) { m_lists[i]->CheckMatch(fullPic,start1,color); if(GetMatchRate(sim) == FALSE) { return FALSE; } } return TRUE; } ULONG OcrNode::CheckMatch(OcrScreen &fullPic, POINT &start1, OCRCOLOR color, ULONG sim, string IN &str) { ClearRecord(); if(CheckMainKeyMatch(fullPic,start1,color,sim) == TRUE) { if(CheckLineKeyMatch(fullPic,start1,color,sim) == TRUE) { if(CheckLineMatch(fullPic,start1,color,sim) == TRUE) { str = m_NodeName; return -1; } } } //do the fast jump return m_lists[m_SundayIndex]->CheckSundayLine(fullPic,start1,color); }