#include "StdAfx.h" #include "ImageReSampler.h" #include "CHImageControls/CHImageProcess.h" using namespace CHImageControls; CImageReSampler::CImageReSampler(int nThreadCount, IImageReSampler2Parent* pIIRS2P) : CWorkThreadPools(nThreadCount), m_pIIRS2P(pIIRS2P) { Reset(); } CImageReSampler::~CImageReSampler(void) { Reset(); } void CImageReSampler::Reset() { m_nSourceWidth = 0; m_nSourceHeight = 0; m_nSourceChannels = 0; m_nSourceFrameHeight = 0; m_nTargetFrameHeight = 0; m_nTargetFrameSize = 0; m_nTotalFrameCount = 0; m_nReSampledFrameCount = 0; m_csTargetImage.Lock(); m_TargetImage.ReleaseImage(); m_csTargetImage.Unlock(); } int CImageReSampler::SetBufferInfo( int nSrcWidth, int nSrcHeight, int nSrcChannels, int nSrcFrameHeight, int nTargetWidth, int nTargetHeight ) { m_csTargetImage.Lock(); int nExtraSize = nSrcHeight % nSrcFrameHeight; if (nExtraSize!=0) { m_csTargetImage.Unlock(); return 0; } // cal frame count m_nSourceFrameHeight = nSrcFrameHeight; m_nTotalFrameCount = nSrcHeight / m_nSourceFrameHeight; int nStep = nTargetWidth % 4; nTargetWidth = nTargetWidth + (4-nStep); //nTargetWidth = ((nTargetWidth+3)/4)*4; // source check BOOL bNotAlloc = m_TargetImage.GetImageExist(); bNotAlloc = bNotAlloc & (m_nSourceWidth==nSrcWidth); m_nSourceWidth = nSrcWidth; bNotAlloc = bNotAlloc & (m_nSourceHeight==nSrcHeight); m_nSourceHeight = nSrcHeight; bNotAlloc = bNotAlloc & (m_nSourceChannels==nSrcChannels); m_nSourceChannels = nSrcChannels; // target check bNotAlloc = bNotAlloc & (m_TargetImage.GetWidth()==nTargetWidth); bNotAlloc = bNotAlloc & (m_TargetImage.GetHeight()==nTargetHeight); bNotAlloc = bNotAlloc & (m_TargetImage.GetChannels()==m_nSourceChannels); m_nTargetFrameSize = (nTargetWidth * nTargetHeight * m_nSourceChannels) / m_nTotalFrameCount; m_nTargetFrameHeight = nTargetHeight / m_nTotalFrameCount; // alloc target image if (bNotAlloc==FALSE) { if (m_TargetImage.CreateImage(nTargetWidth, nTargetHeight, 8, m_nSourceChannels)==FALSE) { m_csTargetImage.Unlock(); return 0; } } // clear target image m_TargetImage.ClearImage(0); m_csTargetImage.Unlock(); return 1; } void CImageReSampler::Start_ReSampling() { m_nReSampledFrameCount = 0; } int CImageReSampler::SetFrameBuffer( int nFrameIndex, BYTE* pFrameBuffer, int nFrameWidth, int nFrameHeight, int nFrameChannels ) { if (m_TargetImage.GetImageExist()==FALSE) return 0; if (nFrameIndex<0 || nFrameIndex >= m_nTotalFrameCount) return 0; CFrameThreadData *pThreadData = new CFrameThreadData(this); if (pThreadData==NULL) return FALSE; if (pThreadData->SetFrameData(nFrameIndex, pFrameBuffer, nFrameWidth, nFrameHeight, nFrameChannels)!=1) { delete pThreadData; } return CreateWorkThread(pThreadData); } int CImageReSampler::SaveImage( const CString& strFilename, int nQual ) { return m_TargetImage.SaveImage(strFilename, nQual); } void CImageReSampler::WorkThreadProcess( PVOID pParameter ) { if (m_TargetImage.GetImageExist()==FALSE) return; if (pParameter==NULL) return; CFrameThreadData* pThreadData = static_cast(pParameter); CCHImageData *pFrameImage = pThreadData->GetFrameImage(); CCHImageData resizeImage; if (resizeImage.CreateImageHeader(m_TargetImage.GetWidth(), m_nTargetFrameHeight, 8, pFrameImage->GetChannels())==FALSE) { return; } char* pResizePtr = m_TargetImage.GetImageBuffer() + (pThreadData->GetIndex() * m_nTargetFrameSize); resizeImage.SetImageDataPtr(pResizePtr); if (CCHImageProcess::ImageResize(pFrameImage, &resizeImage, m_TargetImage.GetWidth(), m_nTargetFrameHeight)!=ResultSuccess) { return; } m_csTargetImage.Lock(); m_nReSampledFrameCount++; if ( m_pIIRS2P && (m_nReSampledFrameCount>=m_nTotalFrameCount) ) { m_pIIRS2P->IIRS2P_ImageReSampleComplete(m_nReSampledFrameCount); } m_csTargetImage.Unlock(); } void CImageReSampler::SetIIRS2P( IImageReSampler2Parent* pIIRS2P ) { m_csTargetImage.Lock(); m_pIIRS2P = pIIRS2P; m_csTargetImage.Unlock(); }