#include "stdafx.h" #include "BSEImg.h" namespace OTSDATA { IMPLEMENT_SERIAL(CBSEImg, CObject, 1) // constructor CBSEImg::CBSEImg() : m_pnImageData(NULL) { // set chat data to 0 Init(); } // constructor CBSEImg::CBSEImg(CRect a_rectImage) : m_pnImageData(NULL) { // set chat data to 0 Init(); // set image rectangle and create memory for image data SetImageRect(a_rectImage); } // copy constructor CBSEImg::CBSEImg(const CBSEImg& a_oSource) : m_pnImageData(NULL) { // can't copy itself if (&a_oSource == this) { return; } // copy data over Duplicate(a_oSource); } // copy constructor CBSEImg::CBSEImg(CBSEImg* a_poSource) : m_pnImageData(NULL) { // input check ASSERT(a_poSource); if (!a_poSource) { return; } // can't copy itself if (a_poSource == this) { return; } // copy data over Duplicate(*a_poSource); } // =operator CBSEImg& CBSEImg::operator=(const CBSEImg& a_oSource) { // cleanup Cleanup(); // copy the class data over Duplicate(a_oSource); // return class return *this; } // destructor CBSEImg::~CBSEImg() { // cleanup Cleanup(); } // CBSEImage functions void CBSEImg::Serialize(CArchive& ar) { if (ar.IsStoring()) { ar << m_rectImage; ar.Write(m_pnImageData, sizeof(BYTE) * m_rectImage.Width() * m_rectImage.Height()); } else { ar >> m_rectImage; InitImageData(m_rectImage.Width(), m_rectImage.Height()); ar.Read(m_pnImageData, sizeof(BYTE) * m_rectImage.Width() * m_rectImage.Height()); } CObject::Serialize(ar); } // public // set image rectangle and create memory for image data void CBSEImg::SetImageRect(const CRect& a_rectImage) { // set chat data to 0 InitChartData(); // set image rectangle m_rectImage = a_rectImage; // create memory for image data InitImageData(a_rectImage.Width(), a_rectImage.Height()); } // Initial image data void CBSEImg::InitImageData(int imgwidth, int imgheight) { // cleanup image data if (m_pnImageData) { delete[]m_pnImageData; m_pnImageData = NULL; } m_rectImage = new CRect(0, 0, imgwidth, imgheight); // create memory for image data m_pnImageData = new BYTE[imgwidth * imgheight]; memset(m_pnImageData, 0, sizeof(BYTE) * imgwidth* imgheight); } // set image data void CBSEImg::SetImageData(BYTE* a_pnImageData,int imgwidth,int imgheight) { if (!a_pnImageData) { return; } // initialize image data InitImageData(imgwidth,imgheight); // copy image data over memcpy(m_pnImageData, a_pnImageData, sizeof(BYTE) * imgwidth * imgheight); } void CBSEImg::SetImageDataPointer(BYTE* a_pnImageData, int imgwidth, int imgheight) { if (!a_pnImageData) { return; } // initialize image data if (m_pnImageData) { delete[]m_pnImageData; m_pnImageData = NULL; } m_rectImage = new CRect(0, 0, imgwidth, imgheight); // copy image data over m_pnImageData = a_pnImageData; } // initial chart data void CBSEImg::InitChartData() { // set chart data to 0 memset(m_nBSEChart, 0, sizeof(WORD) * MAXBYTE); } // set chart data void CBSEImg::SetChartData() { // set chart data to 0 InitChartData(); // make sure that image data is OK if (m_rectImage.IsRectEmpty() || !m_pnImageData) { // No image, return ASSERT(FALSE); return; } // set chart data long nDataSize = m_rectImage.Width() * m_rectImage.Height(); for (long i = 0; i < nDataSize; ++i) { BYTE nValue = m_pnImageData[i]; ++m_nBSEChart[nValue]; } } // calculate chart data BSE high value // return chart data BSE high value, -1 if there is no image // long CBSEImg::CalBSEChartHigh() { // set return to -1 long nRet = -1; // check if there is image if (m_rectImage.IsRectEmpty() || !m_pnImageData) { // NO image, return -1 return nRet; } // calculate chart data BSE high value for (long i = 0; i < MAXBYTE; ++i) { nRet = max(nRet, m_nBSEChart[i]); } // return chart data BSE high value return nRet; } // calculate chart data BSE low value // return chart data BSE low value, -1 if there is no image // long CBSEImg::CalBSEChartLow() { // set return to 255 the highest value long nRet = MAXBYTE; // check if there is image if (m_rectImage.IsRectEmpty() || !m_pnImageData) { // NO image, return -1 return -1; } // calculate chart data BSE low value for (long i = 0; i < MAXBYTE; ++i) { nRet = min(nRet, m_nBSEChart[i]); } // return chart data BSE low value return nRet; } // get point BSE value // return point BSE value, -1 if there is no image or point is not in the image int CBSEImg::GetBSEValue(const CPoint& a_position) { // set return to -1 long nRet = -1; // check if there is image if (m_rectImage.IsRectEmpty() || !m_pnImageData) { // NO image, return -1 return nRet; } // check if the point is in the image if (!m_rectImage.PtInRect(a_position)) { // not in the image, return -1 return nRet; } // note: BSE region should start from 0,0, otherwise need to calculate the position. nRet = m_pnImageData[a_position.x + a_position.y * m_rectImage.Width()]; // return get point BSE value return nRet; } // get BSE value by x, y position values // return point BSE value, -1 if there is no image or position is not in the image int CBSEImg::GetBSEValue(const int a_nX, const int a_nY) { // convert position value to point CPoint pos(a_nX, a_nY); // calculate and return get position BSE value return GetBSEValue(pos); } void CBSEImg::SetBSEValue(const int a_nX, const int a_nY,int value) { // convert position value to point CPoint pos(a_nX, a_nY); // set return to -1 long nRet = -1; // check if there is image if (m_rectImage.IsRectEmpty() || !m_pnImageData) { // NO image, return -1 return ; } // check if the point is in the image if (!m_rectImage.PtInRect(pos)) { // not in the image, return -1 return ; } // note: BSE region should start from 0,0, otherwise need to calculate the position. m_pnImageData[pos.x + pos.y * m_rectImage.Width()]=value; // calculate and return get position BSE value //GetBSEValue(pos); } int CBSEImg::GetValueDirect(const CPoint& a_position) const { return (0 <= a_position.x) && (0 <= a_position.y) && (a_position.x < m_rectImage.Width()) && (a_position.y < m_rectImage.Height()) ? m_pnImageData[a_position.x + a_position.y * m_rectImage.Width()] : 0; } // check if the image contains any given gray level pixel bool CBSEImg::DoesContainPixelValue(int inValue, int a_nPixel /*= 1*/) const { int pixelCount = m_rectImage.Width() * m_rectImage.Height(); int nFindPixel = 0; for (int i = 0; i < pixelCount; ++i) { if (m_pnImageData[i] == inValue) { // find a same gray level pixel ++nFindPixel; if (nFindPixel >= a_nPixel) { return true; } } } return false; } // calculate image area // return image area, return 0 if there no image DWORD CBSEImg::CalArea() { // set return to 0 DWORD nRet = 0; // check image data if (!m_pnImageData) { // return 0 if there no image return nRet; } // calculate image area BYTE* pData = m_pnImageData; for (long i = 0; i < m_rectImage.Width(); ++i) { for (long j = 0; j < m_rectImage.Height(); ++j) { if (*pData != 0) { ++nRet; } ++pData; } } // return calculated area value return nRet; } // protected // cleanup void CBSEImg::Cleanup() { // clean image data if (m_pnImageData) { delete m_pnImageData; m_pnImageData = NULL; } } // initialization void CBSEImg::Init() { m_rectImage.SetRectEmpty(); InitChartData(); } // duplication void CBSEImg::Duplicate(const CBSEImg& a_oSource) { // initialization Init(); // copy data SetImageRect(a_oSource.GetImageRect()); auto r = a_oSource.GetImageRect(); SetImageData(a_oSource.GetImageDataPointer(),r.Width(),r.Height()); } }