// MatchBuffer.cpp: implementation of the CMatchBuffer class.
|
//
|
//////////////////////////////////////////////////////////////////////
|
|
#include "stdafx.h"
|
#include "MatchBuffer.h"
|
#include "SISBuffer.h"
|
|
#ifdef _DEBUG
|
#define new DEBUG_NEW
|
#undef THIS_FILE
|
static char THIS_FILE[] = __FILE__;
|
#endif
|
|
//////////////////////////////////////////////////////////////////////
|
// Construction/Destruction
|
//////////////////////////////////////////////////////////////////////
|
|
#define MOSIS_MEMORY_ALIGN 16
|
|
#if defined(MOSIS_MEMORY_ALIGN)
|
#include "malloc.h"
|
#endif
|
|
CUShortBuff::CUShortBuff()
|
: m_pData(NULL), m_Width(0), m_Height(0), m_DataSpace(0)
|
{}
|
CUShortBuff::~CUShortBuff()
|
{
|
ReleaseSpace();
|
}
|
void CUShortBuff::ReleaseSpace()
|
{
|
if(m_pData)
|
{
|
#if defined(MOSIS_MEMORY_ALIGN)
|
_mm_free(m_pData);
|
#else
|
delete[] m_pData;
|
#endif
|
m_pData= NULL;
|
}
|
m_Width= 0;
|
m_Height= 0;
|
m_DataSpace= 0;
|
}
|
BOOL CUShortBuff::SetSize(int width, int height)
|
{
|
int space= width*height;
|
if(space < 1)
|
return FALSE;
|
|
if(m_DataSpace < space)
|
{
|
ReleaseSpace();
|
}
|
|
if(m_pData == NULL)
|
{
|
#if defined(MOSIS_MEMORY_ALIGN)
|
m_pData= (ushort*)_mm_malloc(space*sizeof(ushort), MOSIS_MEMORY_ALIGN);
|
#else
|
m_pData= new ushort[space];
|
#endif
|
m_DataSpace= space;
|
}
|
m_Width= width;
|
m_Height= height;
|
|
return IsValidBuffer();
|
}
|
|
|
BOOL CUShortBuff::SetBuffPyramid(CSISBuffer fromBuff, CRect roi, int multi)
|
{
|
int width, height;
|
width= (int)(roi.Width()/multi);///8*8;
|
height= roi.Height()/multi;
|
|
ushort data;
|
|
if(! SetSize(width, height))
|
return FALSE;
|
|
int x, y;
|
int divide= multi*multi;
|
// divide= 1;
|
|
for(y= 0; y< height; y++)
|
{
|
for(x= 0; x< width; x++)
|
{
|
data= 0;
|
for(int yy= y*multi; yy< y*multi+ multi; yy++)
|
{
|
for(int xx= x*multi; xx< x*multi+ multi; xx++)
|
{
|
data+= fromBuff.GetPixel(roi.left+ xx, roi.top+ yy);
|
}
|
}
|
SetPixel(x, y, (ushort)((float)data/divide+ 0.5));
|
// for(int xx= x*multi; xx< x*multi+ multi; xx++)
|
// {
|
// data+= fromBuff.GetPixel(roi.left+ xx, roi.top+ xx);
|
// }
|
// SetPixel(x, y, data/multi);
|
}
|
}
|
/*
|
__m128i mmResult, mmResult1, mmResult2;
|
__m128i mmA, mmB;
|
__m128i mmAA, mmBB;
|
__m128i mmZeroData= _mm_setzero_si128();
|
BYTE *pData;
|
multi= 2;
|
int distance= 2<<multi;
|
for(y= 0; y< height; y++)
|
{
|
for(x= 0; x< width- distance; x+=distance)
|
{
|
mmResult= _mm_setzero_si128();
|
for(int i= 0; i< distance; i++)// yÃàÀ¸·Î ÇÕ.
|
{
|
pData= fromBuff.GetDataAddress(x*distance, y*distance+ i);
|
mmA= _mm_loadu_si128((__m128i*) (pData));
|
mmB= _mm_loadu_si128((__m128i*) (pData+ distance*8));
|
|
mmAA= _mm_unpackhi_epi8(mmA, mmZeroData);
|
mmBB= _mm_unpacklo_epi8(mmA, mmZeroData);
|
|
mmResult1= _mm_hadd_epi16(mmAA, mmBB);
|
|
mmAA= _mm_unpackhi_epi8(mmB, mmZeroData);
|
mmBB= _mm_unpacklo_epi8(mmB, mmZeroData);
|
|
mmResult2= _mm_hadd_epi16(mmAA, mmBB);
|
|
mmResult2= _mm_hadd_epi16(mmResult1, mmResult2);
|
|
mmResult= _mm_add_epi16(mmResult, mmResult2);
|
}
|
_mm_storeu_si128((__m128i*)GetDataAddress(x, y), mmResult);
|
}
|
}
|
*/
|
return TRUE;
|
}
|
|
BOOL CUShortBuff::CopyFrom(CUShortBuff &fromBuffer, CRect rect)
|
{
|
if(rect.left < 0 || rect.right > fromBuffer.GetWidth() ||
|
rect.top < 0 || rect.bottom > fromBuffer.GetHeight())
|
return FALSE;
|
if(! IsValidBuffer())
|
return FALSE;
|
|
if(GetHeight() < rect.Height())
|
return FALSE;
|
|
for(int i= 0; i< rect.Height(); i++)
|
{
|
memcpy(GetDataAddress(0, i), fromBuffer.GetDataAddress(rect.left, i+ rect.top), GetWidth()*sizeof(ushort));
|
}
|
return TRUE;
|
}
|
|
|
void CByteBuff::ReleaseSpace()
|
{
|
if(m_pData)
|
{
|
#if defined(MOSIS_MEMORY_ALIGN)
|
_mm_free(m_pData);
|
#else
|
delete[] m_pData;
|
#endif
|
}
|
m_pData= NULL;
|
m_Width= 0;
|
m_Height= 0;
|
m_DataSpace= 0;
|
}
|
BOOL CByteBuff::SetSize(int width, int height)
|
{
|
int space= width*height;
|
if(space < 1)
|
return FALSE;
|
|
if(m_DataSpace < space)
|
{
|
ReleaseSpace();
|
}
|
|
if(m_pData == NULL)
|
{
|
#if defined(MOSIS_MEMORY_ALIGN)
|
m_pData= (BYTE*)_mm_malloc(space*sizeof(BYTE), MOSIS_MEMORY_ALIGN);
|
#else
|
m_pData= new BYTE[space];
|
#endif
|
m_DataSpace= space;
|
}
|
|
m_Width= width;
|
m_Height= height;
|
return IsValidBuffer();
|
}
|
BOOL CByteBuff::CopyFrom(CSISBuffer &fromBuffer, CRect rect)
|
{
|
// ¹öÆÛ üũ¸¦ ÇÏÁö ¾Ê°Ú´Ù... ¶óÀο¡¼´Â ¹öÆÛ°¡ ºÙ¾î ÀÖÀ¸´Ï±î..
|
// ±Û¶ó½º TILT¿¡ ÀÇÇØ Á¡ÁøÀû ¸ÅĪ¿¡¼ ¹öÆÛ °æ°è¸¦ ÃʰúÇÒ ¼ö ÀÖ´Ù.
|
// if(rect.left < 0 || rect.right > fromBuffer.GetWidth() ||
|
// rect.top < 0 || rect.bottom > fromBuffer.GetHeight())
|
// return FALSE;
|
if(! IsValidBuffer())
|
return FALSE;
|
|
if(GetHeight() < rect.Height())
|
return FALSE;
|
|
int width= rect.Width();
|
if(GetWidth() < width)
|
width= GetWidth();
|
if(rect.left < 0)
|
return FALSE;
|
if(rect.top < 0)
|
return FALSE;
|
if(rect.right > fromBuffer.GetWidth())
|
return FALSE;
|
if(rect.bottom > fromBuffer.GetHeight())
|
return FALSE;
|
|
for(int i= 0; i< rect.Height(); i++)
|
{
|
memcpy(GetDataAddress(0, i), fromBuffer.GetDataAddress(rect.left, i+ rect.top), width);
|
}
|
return TRUE;
|
/* int width, height;
|
width= rect.Width();
|
height= rect.Height();
|
if(SetSize(rect.Width(), rect.Height()))
|
{
|
for(int yy= 0; yy< rect.Height(); yy++)
|
{
|
memcpy(GetDataAddress(0, yy), fromBuffer.GetDataAddress(rect.left, rect.top+ yy), rect.Width());
|
}
|
}*/
|
}
|
BOOL CByteBuff::CopyFrom(CByteBuff &fromBuffer, CRect rect)
|
{
|
if(rect.left < 0 || rect.right > fromBuffer.GetWidth() ||
|
rect.top < 0 || rect.bottom > fromBuffer.GetHeight())
|
return FALSE;
|
if(! IsValidBuffer())
|
return FALSE;
|
|
if(GetHeight() < rect.Height())
|
return FALSE;
|
|
int width= rect.Width();
|
if(GetWidth() < width)
|
width= GetWidth();
|
if(rect.left < 0)
|
return FALSE;
|
if(rect.top < 0)
|
return FALSE;
|
if(rect.right > fromBuffer.GetWidth())
|
return FALSE;
|
if(rect.bottom > fromBuffer.GetHeight())
|
return FALSE;
|
|
for(int i= 0; i< rect.Height(); i++)
|
{
|
memcpy(GetDataAddress(0, i), fromBuffer.GetDataAddress(rect.left, i+ rect.top), width);
|
}
|
return TRUE;
|
/* if(SetSize(rect.Width(), rect.Height()))
|
{
|
for(int yy= 0; yy< rect.Height(); yy++)
|
{
|
memcpy(GetDataAddress(0, yy), fromBuffer.GetDataAddress(rect.left, rect.top+ yy), rect.Width());
|
}
|
}*/
|
}
|
|
BOOL CByteBuff::SetBuffPyramid(CSISBuffer fromBuff, CRect roi, int multi)
|
{
|
int width, height;
|
width= (int)(roi.Width()/multi);///8*8;
|
height= roi.Height()/multi;
|
|
ushort data;
|
|
if(! SetSize(width, height))
|
return FALSE;
|
|
int x, y;
|
int divide= multi*multi;
|
|
for(y= 0; y< height; y++)
|
{
|
for(x= 0; x< width; x++)
|
{
|
data= 0;
|
for(int yy= y*multi; yy< y*multi+ multi; yy++)
|
{
|
for(int xx= x*multi; xx< x*multi+ multi; xx++)
|
{
|
data+= fromBuff.GetPixel(roi.left+ xx, roi.top+ yy);
|
}
|
}
|
SetPixel(x, y, (BYTE)((float)data/divide+ 0.5));
|
// for(int xx= x*multi; xx< x*multi+ multi; xx++)
|
// {
|
// data+= fromBuff.GetPixel(roi.left+ xx, roi.top+ xx);
|
// }
|
// SetPixel(x, y, data/multi);
|
}
|
}
|
/*
|
__m128i mmResult, mmResult1, mmResult2;
|
__m128i mmA, mmB;
|
__m128i mmAA, mmBB;
|
__m128i mmZeroData= _mm_setzero_si128();
|
BYTE *pData;
|
multi= 2;
|
int distance= 2<<multi;
|
for(y= 0; y< height; y++)
|
{
|
for(x= 0; x< width- distance; x+=distance)
|
{
|
mmResult= _mm_setzero_si128();
|
for(int i= 0; i< distance; i++)// yÃàÀ¸·Î ÇÕ.
|
{
|
pData= fromBuff.GetDataAddress(x*distance, y*distance+ i);
|
mmA= _mm_loadu_si128((__m128i*) (pData));
|
mmB= _mm_loadu_si128((__m128i*) (pData+ distance*8));
|
|
mmAA= _mm_unpackhi_epi8(mmA, mmZeroData);
|
mmBB= _mm_unpacklo_epi8(mmA, mmZeroData);
|
|
mmResult1= _mm_hadd_epi16(mmAA, mmBB);
|
|
mmAA= _mm_unpackhi_epi8(mmB, mmZeroData);
|
mmBB= _mm_unpacklo_epi8(mmB, mmZeroData);
|
|
mmResult2= _mm_hadd_epi16(mmAA, mmBB);
|
|
mmResult2= _mm_hadd_epi16(mmResult1, mmResult2);
|
|
mmResult= _mm_add_epi16(mmResult, mmResult2);
|
}
|
_mm_storeu_si128((__m128i*)GetDataAddress(x, y), mmResult);
|
}
|
}
|
*/
|
return TRUE;
|
}
|