#include "StdAfx.h"
|
#include "SharedImageData.h"
|
|
CSharedImageData::CSharedImageData(void)
|
{
|
m_pSMImageData = NULL;
|
m_nImageCount = 0;
|
m_nImageInfoSize = sizeof(SSharedImageInfo);
|
//m_nMaxImageSize = 2456 * 2058 * 4;
|
m_nMaxImageSize = 2464 * 2056 * 4;
|
m_hAsignedMemory = NULL;
|
m_pSharedMemory = NULL;
|
m_pImageInfo = NULL;
|
m_pImageData = NULL;
|
}
|
|
CSharedImageData::~CSharedImageData(void)
|
{
|
DeleteMemory();
|
}
|
|
void CSharedImageData::DeleteMemory()
|
{
|
if(m_pSharedMemory)
|
{
|
UnmapViewOfFile(m_pSharedMemory);
|
m_pSharedMemory = NULL;
|
}
|
|
if(m_hAsignedMemory)
|
{
|
CloseHandle(m_hAsignedMemory);
|
m_hAsignedMemory = NULL;
|
}
|
|
if (m_pSMImageData)
|
{
|
for (int i=0; i<m_nImageCount; i++)
|
{
|
delete m_pSMImageData[i];
|
}
|
delete [] m_pSMImageData;
|
m_pSMImageData = NULL;
|
}
|
|
if (m_pImageInfo)
|
{
|
delete [] m_pImageInfo;
|
m_pImageInfo = NULL;
|
}
|
|
if (m_pImageData)
|
{
|
delete [] m_pImageData;
|
m_pImageData = NULL;
|
}
|
|
m_nImageCount = 0;
|
|
}
|
|
BOOL CSharedImageData::CreateMemory(int nImageCount)
|
{
|
DeleteMemory();
|
|
m_nImageCount = nImageCount;
|
|
// alloc semapore
|
m_pSMImageData = new CSemaphore*[m_nImageCount];
|
m_pImageInfo = new SSharedImageInfo*[m_nImageCount];
|
m_pImageData = new BYTE*[m_nImageCount];
|
|
for (int i=0; i<m_nImageCount; i++)
|
{
|
m_pSMImageData[i] = new CSemaphore(1, 1);
|
m_pImageInfo[i] = NULL;
|
m_pImageData[i] = NULL;
|
}
|
|
long nTotalImageInfoSize = m_nImageInfoSize * m_nImageCount;
|
long nTotalImageSize = m_nMaxImageSize * m_nImageCount;
|
long nTotalAsignedBuffSize = nTotalImageInfoSize + nTotalImageSize;
|
|
m_hAsignedMemory = (HANDLE) CreateFileMapping(
|
INVALID_HANDLE_VALUE,
|
NULL,
|
PAGE_READWRITE,
|
0,
|
(DWORD)nTotalAsignedBuffSize,
|
_T("SHARED_IMAGE_DATA"));
|
|
if(!m_hAsignedMemory) return FALSE;
|
|
m_pSharedMemory = (BYTE *)MapViewOfFile(m_hAsignedMemory, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
|
|
if(!m_pSharedMemory) return FALSE;
|
|
for (int i=0; i<m_nImageCount; i++)
|
{
|
m_pImageInfo[i] = (SSharedImageInfo*)( m_pSharedMemory + (m_nImageInfoSize*i) );
|
|
m_pImageData[i] = (BYTE *)( m_pSharedMemory + nTotalImageInfoSize + (m_nMaxImageSize*i) );
|
}
|
|
return TRUE;
|
}
|
|
|
|
int CSharedImageData::GetUpdated(int nIndex)
|
{
|
if (nIndex<0 || nIndex>=m_nImageCount) return 0;
|
|
if (m_pImageInfo[nIndex])
|
{
|
return m_pImageInfo[nIndex]->nUpdated;
|
}
|
|
return 0;
|
}
|
|
int CSharedImageData::GetSelected(int nIndex)
|
{
|
if (nIndex<0 || nIndex>=m_nImageCount) return 0;
|
|
if (m_pImageInfo[nIndex])
|
{
|
return m_pImageInfo[nIndex]->nSelected;
|
}
|
|
return 0;
|
}
|
|
|
BOOL CSharedImageData::LockImageData(int nIndex)
|
{
|
if (nIndex<0 || nIndex>=m_nImageCount) return FALSE;
|
|
if (m_pSMImageData[nIndex])
|
{
|
m_pSMImageData[nIndex]->Lock();
|
}
|
else
|
{
|
return FALSE;
|
}
|
|
return TRUE;
|
}
|
|
BOOL CSharedImageData::UnlockImageData(int nIndex)
|
{
|
if (nIndex<0 || nIndex>=m_nImageCount) return FALSE;
|
|
if (m_pImageInfo[nIndex])
|
{
|
m_pImageInfo[nIndex]->nUpdated--;
|
}
|
else
|
{
|
return FALSE;
|
}
|
|
if (m_pSMImageData[nIndex])
|
{
|
m_pSMImageData[nIndex]->Unlock();
|
}
|
else
|
{
|
return FALSE;
|
}
|
|
return TRUE;
|
}
|
|
const BYTE* CSharedImageData::GetImageData(int nIndex)
|
{
|
if (nIndex<0 || nIndex>=m_nImageCount) return NULL;
|
|
return m_pImageData[nIndex];
|
}
|
|
const SSharedImageInfo* CSharedImageData::GetImageInfo(int nIndex)
|
{
|
if (nIndex<0 || nIndex>=m_nImageCount) return NULL;
|
|
return m_pImageInfo[nIndex];
|
}
|
|
BOOL CSharedImageData::SetImageData(int nIndex, SSharedImageInfo *pImageInfo, BYTE* pImageData)
|
{
|
if (pImageInfo==NULL || pImageData==NULL) return FALSE;
|
|
if(m_hAsignedMemory==NULL) return FALSE;
|
|
if (nIndex<0 || nIndex>=m_nImageCount) return FALSE;
|
|
int nSrcSize = pImageInfo->nImageWidthStep * pImageInfo->nImageHeight;
|
|
if (nSrcSize>m_nMaxImageSize) return FALSE;
|
|
if (m_pSMImageData[nIndex]==NULL) return FALSE;
|
|
m_pSMImageData[nIndex]->Lock(); // 함수를 Atomic 하게 동작시키자
|
|
// 이미지 정보 기록
|
pImageInfo->nUpdated = 1;
|
memcpy(m_pImageInfo[nIndex], pImageInfo, m_nImageInfoSize);
|
|
// 이미지 기록
|
memcpy(m_pImageData[nIndex], pImageData, nSrcSize);
|
|
m_pSMImageData[nIndex]->Unlock();// 함수 동기화 종료
|
|
return TRUE;
|
}
|