SDC C-Project CF Review 프로그램
LYW
2021-07-23 55615eba335d4cbc1f83330dc5078fe073034b7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#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<CFrameThreadData*>(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();
}