www.pudn.com > try2LPR-1.0.rar > RecoHelper.cpp


/* 
* Copyright (c) 2003, try2it.com 
* All rights reserved. 
* 
* This program is free software; you can redistribute it and/or modify it under 
* the terms of the GNU General Public License as published by the Free Software 
* Foundation; either version 2 of the License, or (at your option) any later 
* version. 
*  
* This program is distributed in the hope that it will be useful, but WITHOUT 
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
* details. 
*  
* You should have received a copy of the GNU General Public License along with 
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple 
* Place - Suite 330, Boston, MA 02111-1307, USA. 
* 
* 文件名称:RecoHelper.cpp 
* 文件标识:LPR-03-03 
* 摘要:牌照识别的内部助手函数 
* 
* 当前版本:1.0 
* 作者:try2it.com 
* 开始日期:2003年09月27日 
* 完成日期:2003年10月10日 
*/ 
#include "stdafx.h" 
#include  
#include  
#include  
#include "../include/types.h" 
#include "RecoHelper.h" 
#include "resource.h" 
/*------------------------------------------------------------------ 
* 以下声明全局变量 
------------------------------------------------------------------*/ 
PMemNode g_pMemHead = NULL;  
PMemNode g_pMemTail = NULL;   
int g_nMemCount = 0;       
 
int LoadTemplate(HMODULE hModule, PGrayImg pGrayImg, int index) 
{ 
    HRSRC hResInfo; 
	HGLOBAL hRes; 
	int res = 1; 
	DWORD dwResSize;               
	DWORD dwBufferSize;            
	BYTE* pData = NULL;            
	BYTE *buf = NULL;           
 
	__try 
	{ 
		hResInfo = FindResource(hModule, (LPCTSTR)(IDB_0+index), "DLL" ); 
		hRes = LoadResource(hModule, hResInfo ); 
		dwResSize = SizeofResource(hModule, hResInfo ); 
		pData = (BYTE*)LockResource(hRes); 
		dwBufferSize = dwResSize - sizeof(BITMAPFILEHEADER); 
		if ((buf = (BYTE*)malloc(dwBufferSize))==NULL) 
		{ 
			res = 0; 
			__leave; 
		} 
 
		memcpy(buf, pData+sizeof(BITMAPFILEHEADER), dwBufferSize); 
		res = LoadImgToArray(pGrayImg, buf);		 
	} 
	__finally 
	{ 
		if (buf) 
			free(buf); 
	} 
	return res; 
} 
 
 
int LoadImgToArray(PGrayImg pGrayImg, BYTE *pImgBuff) 
{ 
	BYTE *pData, *pColor; 
	BYTE Y, Red, Blue, Green; 
	BITMAPINFOHEADER Bi; 
	RGBQUAD MyRGB; 
	int res = 0; 
	int i, j; 
 
	WORD wLineBytes; 
 
	memcpy(&Bi, pImgBuff, sizeof(BITMAPINFOHEADER)); 
 
	if ((res = GrayImg_Malloc(pGrayImg, Bi.biWidth, Bi.biHeight))==0) 
	{ 
		return 0; 
	} 
 
	wLineBytes = (int)((Bi.biWidth * Bi.biBitCount + 31) / 32) * 4; 
	Bi.biSizeImage = wLineBytes * Bi.biHeight; 
 
	switch (Bi.biBitCount) 
	{ 
	case 24: 
		for (i=0; ipImg+i*Bi.biWidth+j) = Y; 
			} 
		} 
 
		break; 
 
	case 16: 
 
		break; 
 
	case 8: 
		for (i=0; ipImg+i*Bi.biWidth+j) = Y; 
			} 
		} 
 
		break; 
 
	case 4: 
		for (i=0; i> (4 * ((j + 1) % 2)) & (BYTE)15); 
 
				memcpy(&MyRGB, pColor + Y * sizeof(RGBQUAD), sizeof(RGBQUAD)); 
				 
                Blue = MyRGB.rgbBlue; 
				Green = MyRGB.rgbGreen; 
				Red = MyRGB.rgbRed; 
				Y = (int)(0.299 * Red + 0.587 * Green+ 0.114 * Blue + 0.5); 
 
				*(pGrayImg->pImg+i*Bi.biWidth+j) = Y; 
			} 
		} 
 
		break; 
 
	case 1: 
		for (i=0; i> (7 - j % 8) & (BYTE)1); 
 
				memcpy(&MyRGB, pColor + Y * sizeof(RGBQUAD), sizeof(RGBQUAD)); 
				 
                Blue = MyRGB.rgbBlue; 
				Green = MyRGB.rgbGreen; 
				Red = MyRGB.rgbRed; 
				Y = (int)(0.299 * Red + 0.587 * Green+ 0.114 * Blue + 0.5); 
				*(pGrayImg->pImg+i*Bi.biWidth+j) = Y; 
			} 
		} 
 
		break; 
 
	}	   
 
	return res; 
} 
 
int ColorImg_Malloc(PColorImg pColorImg, int Width, int Height) 
{ 
	if (pColorImg == NULL) 
		return 0; 
	 
	if (Width == 0 || Height == 0) 
		return 0; 
 
	pColorImg->pYImg = (BYTE*)malloc(Width * Height); 
	if (pColorImg->pYImg == NULL) 
		return 0; 
 
	pColorImg->pRImg = (BYTE*)malloc(Width * Height); 
	if (pColorImg->pRImg == NULL) 
	{ 
		free(pColorImg->pYImg); 
		return 0; 
	} 
 
	pColorImg->pBImg = (BYTE*)malloc(Width * Height); 
	if (pColorImg->pBImg == NULL) 
	{ 
		free(pColorImg->pYImg); 
		free(pColorImg->pRImg); 
		return 0; 
	} 
 
	pColorImg->pGImg = (BYTE*)malloc(Width * Height); 
	if (pColorImg->pGImg == NULL) 
	{ 
		free(pColorImg->pYImg); 
		free(pColorImg->pRImg); 
		free(pColorImg->pBImg); 
		return 0; 
	} 
 
	if (_InsertMemNode(pColorImg->pYImg) == 0) 
		return 0; 
	if (_InsertMemNode(pColorImg->pRImg) == 0) 
		return 0; 
	if (_InsertMemNode(pColorImg->pGImg) == 0) 
		return 0; 
	if (_InsertMemNode(pColorImg->pBImg) == 0) 
		return 0; 
 
	pColorImg->Width = Width; 
	pColorImg->Height = Height; 
 
	return 1; 
} 
 
int GrayImg_Malloc(PGrayImg pGrayImg, int Width, int Height) 
{ 
	if (pGrayImg == NULL) 
		return 0; 
 
	if (Width == 0 || Height == 0) 
		return 0; 
	 
	pGrayImg->pImg = (BYTE*)malloc(Width * Height); 
	if (pGrayImg->pImg == NULL) 
		return 0; 
 
	if (_InsertMemNode(pGrayImg->pImg) == 0) 
		return 0; 
 
	pGrayImg->Width = Width; 
	pGrayImg->Height = Height; 
 
	return 1; 
} 
 
int ColorImg_Free(PColorImg pColorImg) 
{ 
	int res = 0; 
 
	if (pColorImg == NULL) 
		return 1; 
 
	if (pColorImg->pYImg != NULL) 
	{ 
		res = _FreeMemNode(pColorImg->pYImg); 
	} 
	if (pColorImg->pRImg != NULL) 
	{ 
		res = _FreeMemNode(pColorImg->pRImg); 
	} 
	if (pColorImg->pGImg != NULL) 
	{ 
		res = _FreeMemNode(pColorImg->pGImg); 
	} 
	if (pColorImg->pBImg != NULL) 
	{ 
		res = _FreeMemNode(pColorImg->pBImg); 
	} 
 
	return res; 
} 
 
int GrayImg_Free(PGrayImg pGrayImg) 
{ 
	int res = 0; 
 
	if (pGrayImg == NULL) 
		return 1; 
 
	if (pGrayImg->pImg != NULL) 
	{ 
		res = _FreeMemNode(pGrayImg->pImg); 
	} 
 
	return res; 
} 
 
int _InsertMemNode(BYTE *pMem) 
{ 
	PMemNode pMemNode = NULL; 
 
	pMemNode = (PMemNode)malloc(sizeof(MemNode)); 
	if (pMemNode == NULL) 
	{ 
		return 0; 
	} 
	pMemNode->pMem = pMem; 
	pMemNode->next = NULL; 
	pMemNode->prev = NULL; 
 
	if (g_pMemHead == NULL && g_pMemTail == NULL) 
	{ 
		g_pMemHead = (PMemNode)malloc(sizeof(MemNode)); 
		g_pMemTail = (PMemNode)malloc(sizeof(MemNode));   
		 
		g_pMemHead->next = g_pMemTail; 
		g_pMemHead->prev = NULL; 
		g_pMemHead->pMem = NULL; 
 
		g_pMemTail->next = NULL; 
		g_pMemTail->prev = g_pMemHead; 
		g_pMemTail->pMem = NULL; 
	} 
 
	pMemNode->next = g_pMemTail; 
	pMemNode->prev = g_pMemTail->prev; 
 
	g_pMemTail->prev->next = pMemNode; 
	g_pMemTail->prev = pMemNode; 
	 
	g_nMemCount++; 
 
	return 1; 
} 
 
int _FreeMemNode(BYTE *pMem) 
{ 
	PMemNode p; 
 
	p = g_pMemTail->prev; 
 
	while (p != g_pMemHead) 
	{ 
		if (p->pMem == pMem) 
		{ 
			p->prev->next = p->next; 
			p->next->prev = p->prev; 
 
			free(p); 
			p = NULL; 
 
			break; 
		} 
		p = p->prev; 
	} 
 
	if (pMem != NULL) 
	{ 
		free(pMem); 
		pMem = NULL; 
	} 
 
	g_nMemCount--; 
 
	return 1; 
} 
 
int FreeAllMemNode() 
{ 
	PMemNode p,	q; 
 
	if (g_pMemTail==NULL || g_pMemHead==NULL) 
		return 1; 
 
	p = g_pMemTail->prev; 
 
	while (p != g_pMemHead) 
	{ 
		if (p->pMem) 
		{ 
			free(p->pMem); 
			p->pMem = NULL; 
		} 
        q = p; 
		p = p->prev; 
		free(q); 
		q = NULL; 
 
		g_nMemCount--; 
	} 
 
	if (g_pMemTail != NULL) 
	{ 
		free(g_pMemTail); 
		g_pMemTail = NULL; 
	} 
 
	if (g_pMemHead != NULL) 
	{ 
		free(g_pMemHead); 
		g_pMemHead = NULL; 
	} 
 
	return 1; 
}