www.gusucode.com > 基于C++的TRY2LPR-1.0开源车牌识别核心-源码程序 > 基于C++的TRY2LPR-1.0开源车牌识别核心-源码程序\code\LPR\LPRHelper.cpp

    //Download by http://www.NewXing.com
/*
* 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.
*
* 文件名称:LPRHelper.cpp
* 文件标识:LPR-02-03
* 摘要:牌照识别的内部助手函数
*
* 当前版本:1.0
* 作者:try2it.com
* 开始日期:2003年09月27日
* 完成日期:2003年09月30日
*/
#include "stdafx.h"
#include <malloc.h>
#include <stdlib.h>
#include "../include/types.h"
#include "LPRHelper.h"

/*------------------------------------------------------------------
* 以下声明全局变量
------------------------------------------------------------------*/
PMemNode g_pMemHead = NULL;  
PMemNode g_pMemTail = NULL;  
int g_nMemCount = 0;         

int LoadImgToArray(PColorImg pColorImg, 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 = ColorImg_Malloc(pColorImg, Bi.biWidth, Bi.biHeight))==0)
	{
		return 0;
	}
	
	pColorImg->BitCount = Bi.biBitCount;

	wLineBytes = (int)((Bi.biWidth * Bi.biBitCount + 31) / 32) * 4;
	Bi.biSizeImage = wLineBytes * Bi.biHeight;

	switch (Bi.biBitCount)
	{
	case 24:
		for (i=0; i<Bi.biHeight; i++)
		{
			pData = pImgBuff + sizeof(BITMAPINFOHEADER) + 
				          Bi.biClrUsed * sizeof(RGBQUAD) +
				        (Bi.biSizeImage - (WORD)(i + 1) * wLineBytes);

			for (j=0; j<Bi.biWidth; j++)
			{
				Blue = (BYTE)(*pData++);
				Green = (BYTE)(*pData++);
				Red = (BYTE)(*pData++);
				Y = (int)(0.299 * Red + 0.587 * Green+ 0.114 * Blue + 0.5);

				*(pColorImg->pBImg+i*Bi.biWidth+j) = Blue;
				*(pColorImg->pGImg+i*Bi.biWidth+j) = Green;
				*(pColorImg->pRImg+i*Bi.biWidth+j) = Red;
				*(pColorImg->pYImg+i*Bi.biWidth+j) = Y;
			}
		}

		break;

	case 16:

		break;

	case 8:
		for (i=0; i<Bi.biHeight; i++)
		{
			pColor = pImgBuff + sizeof(BITMAPINFOHEADER);
			pData = pImgBuff + sizeof(BITMAPINFOHEADER) + 
				        Bi.biClrUsed * sizeof(RGBQUAD) +
			 	        (Bi.biSizeImage - (WORD)(i + 1) * wLineBytes);

			for (j=0; j<Bi.biWidth; j++)
			{
				Y = *pData++;
				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);
				*(pColorImg->pBImg+i*Bi.biWidth+j) = Blue;
				*(pColorImg->pGImg+i*Bi.biWidth+j) = Green;
				*(pColorImg->pRImg+i*Bi.biWidth+j) = Red;
				*(pColorImg->pYImg+i*Bi.biWidth+j) = Y;
			}
		}

		break;

	case 4:
		for (i=0; i<Bi.biHeight; i++)
		{
			pColor = pImgBuff + sizeof(BITMAPINFOHEADER);
			pData = pImgBuff + sizeof(BITMAPINFOHEADER) + 
				        Bi.biClrUsed * sizeof(RGBQUAD) +
			 	        (Bi.biSizeImage - (WORD)(i + 1) * wLineBytes);

			for (j=0; j<Bi.biWidth; j++)
			{
                if ((j != 0) && (j % 2 == 0))
	                pData++;

                Y = (BYTE)(*pData);
                Y = (BYTE)(Y >> (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);

				*(pColorImg->pBImg+i*Bi.biWidth+j) = Blue;
				*(pColorImg->pGImg+i*Bi.biWidth+j) = Green;
				*(pColorImg->pRImg+i*Bi.biWidth+j) = Red;
				*(pColorImg->pYImg+i*Bi.biWidth+j) = Y;
			}
		}

		break;

	case 1:
		for (i=0; i<Bi.biHeight; i++)
		{
			pColor = pImgBuff + sizeof(BITMAPINFOHEADER);
			pData = pImgBuff + sizeof(BITMAPINFOHEADER) + 
				        Bi.biClrUsed * sizeof(RGBQUAD) +
			 	        (Bi.biSizeImage - (WORD)(i + 1) * wLineBytes);

			for (j=0; j<Bi.biWidth; j++)
			{
                if ((j != 0) && (j % 8 == 0))
					pData++;
                Y = (BYTE)(*pData);
                Y = BYTE(Y >> (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);
				*(pColorImg->pBImg+i*Bi.biWidth+j) = Blue;
				*(pColorImg->pGImg+i*Bi.biWidth+j) = Green;
				*(pColorImg->pRImg+i*Bi.biWidth+j) = Red;
				*(pColorImg->pYImg+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;
}

int ShowImg(HDC hdc, PColorImg pColorImg)
{
	BITMAPINFOHEADER Bi;
	BYTE *pImgBuff = NULL;
	BYTE *pData = NULL;
	WORD wLineBytes;
	int i, j;
	BOOL bSuccess;
	RECT rect;

	Bi.biSize = sizeof(BITMAPINFOHEADER);
    Bi.biWidth = pColorImg->Width;
    Bi.biHeight = pColorImg->Height;
    Bi.biPlanes = 1;
    Bi.biBitCount = 24;
    Bi.biCompression = 0;
    Bi.biClrUsed = 0;
    Bi.biClrImportant = 0;
	wLineBytes = (int)((Bi.biWidth * Bi.biBitCount+31) / 32) * 4;
	Bi.biSizeImage = wLineBytes*Bi.biHeight;
	
	pImgBuff = (BYTE*)malloc(Bi.biSizeImage + sizeof(BITMAPINFOHEADER));
	pData = pImgBuff;
	memcpy(pData, &Bi, sizeof(BITMAPINFOHEADER));
	for (i=0; i<pColorImg->Height-1; i++)
	{
		pData = pImgBuff + sizeof(BITMAPINFOHEADER) +
			       (Bi.biSizeImage - (WORD)(1 + i) * wLineBytes);

		for (j=0; j<pColorImg->Width-1; j++)
		{
			*pData++ = *(pColorImg->pBImg + i * pColorImg->Width + j);
			*pData++ = *(pColorImg->pGImg + i * pColorImg->Width + j);
			*pData++ = *(pColorImg->pRImg + i * pColorImg->Width + j);
		}
	}
	pData = pImgBuff + sizeof(BITMAPINFOHEADER);
	
	rect.left = 0;
	rect.top = 0;
	rect.right = 200;
	rect.bottom = 400;

	FillRect(hdc, &rect, (HBRUSH) (COLOR_WINDOW));
	bSuccess = SetDIBitsToDevice(hdc,                    // HDC
		                         0,                      // DestX
            				     0,                      // DestY
					             pColorImg->Width,       // DestWidth
					             pColorImg->Height,      // DestHeight
					             0,                      // SrcX
					             0,                      // SrcY
					             0,                      // StartScan
					             pColorImg->Height,      // NumScans
                                 pData,                  // Bits
					             (LPBITMAPINFO)pImgBuff, // BitsInfo
					             DIB_RGB_COLORS);        // wUsage

	if (pImgBuff)
		free(pImgBuff);
						  
	return (int)bSuccess;
}

TGrayImg ColorToGray(TColorImg ColorImg)
{
	TGrayImg GrayImg;

	GrayImg.Width = ColorImg.Width;
	GrayImg.Height = ColorImg.Height;
	GrayImg.pImg = ColorImg.pYImg;

	return GrayImg;
}


TColorImg GrayToColor(TGrayImg GrayImg)
{
	TColorImg ColorImg;

	ColorImg.Width = GrayImg.Width;
	ColorImg.Height = GrayImg.Height;
	ColorImg.pYImg = GrayImg.pImg;
	ColorImg.pRImg = GrayImg.pImg;
	ColorImg.pGImg = GrayImg.pImg;
	ColorImg.pBImg = GrayImg.pImg;

	return ColorImg;
}