SDC C-Project CF Review 프로그램
LYW
2021-07-01 4acd943c6f0beecd3ee573f77d8d6c7524fd5045
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#pragma once
 
#include "MosisAssem/AssemParam.h"
#include "MosisBuffer/MosisBuffer.h"
#include <math.h>
#include "MosisStorage.h"
 
class CReinspect;
 
 
typedef short SubMulti;    // ¼­ºêÇȼ¿ÀÇ ´ÜÀ§. 1/16 ¼­ºêÇȼ¿À» »ç¿ë.
typedef short ImgDiff;    // Â÷¿µ»ó À̹ÌÁöÀÇ °ªÀ» ÀúÀåÇҠŸÀÔ - short·Î ÀúÀå. ¸¸¾à ¿¬»êÀÌ ´Þ¶óÁú °æ¿ì °¢ ¿¬»êÀÇ °ªÀÌ short¸¦ ³ÑÁö ¾Ê³ª È®ÀΠÇÊ¿ä.
 
 
class CNoiseStat
{
public:
    CNoiseStat(){m_Mean= 0; m_StdDeviation= 0; m_NoiseTh= 10; m_Rough= 0;}
 
    float    m_Mean;
    float    m_StdDeviation;
    float    m_NoiseTh;        // ÀÚ¿¬ ³ëÀÌÁ ¹èÁ¦ Çϱâ À§ÇÑ ÀÓ°èÄ¡
    float    m_Rough;
    void    Reset()    {m_Mean= 0; m_StdDeviation= 0;}
};
 
 
class CDiffBuff : public CMosisSquare<short>
{
public:
    BOOL MakeDiffBuff(CMosisBuffer &src, CMosisBuffer &cmp, SubMulti s1, SubMulti s2, BOOL bVert= FALSE)
    {
        SetSize(src.GetWidth(), src.GetHeight());
 
        BYTE *pSrc, *pCmp;
        short    *pDiff;
        int    pixelDist= 1;
 
        int x, y;
        SubMulti ssum;
        ssum= s1+ s2;
 
        ASSERT(ssum > 1);
        short rslt;
 
        if(bVert)
            pixelDist= cmp.GetDataWidth();
 
        for(y= 0; y< GetHeight(); y++)
        {
            x= 0;
            pSrc= src.GetDataAddress(x, y);
            pCmp= cmp.GetDataAddress(x, y);
            pDiff= GetData(x, y);
            for(; x< GetWidth(); x++)
            {
                rslt= *pSrc;
                rslt= rslt- (s1*(*pCmp)+ s2*(*(pCmp+pixelDist)))/ssum;
                *pDiff= rslt;
 
                pSrc++;
                pCmp++;
                pDiff++;
            }
        }
        return TRUE;
    }
 
    BOOL GetStatic(CNoiseStat &out, short* pData, int count)
    {
        INT64    sum= 0;
        int        realCount= 0;
        float noise= out.m_NoiseTh;
        short*    pDiff= pData;
 
        for(int x= 0; x< count; x++)
        {
            if(abs(*pDiff) > noise)
            {
                sum+= *pDiff;
                realCount++;
            }
            pDiff++;
        }
 
        if(realCount < 1)
        {
            out.Reset();
            return FALSE;
        }
 
        float mean= (float)sum;
        out.m_Mean= mean= mean/realCount;
 
        float diff, fsum= 0;
        pDiff= pData;
        int rough, roughsum= 0;
        for(int x= 0; x< count; x++)
        {
            if(abs(*pDiff) > noise)
            {
                diff= *pDiff;
                diff-= mean;
                diff= diff*diff;
                fsum+= diff;
            }
            if(x > 0)
                roughsum+= abs(rough- *pDiff);
            rough= *pDiff;
            pDiff++;
        }
        if(count > 2)
            out.m_Rough= (float)roughsum/realCount;
        fsum/=realCount;
        out.m_StdDeviation= sqrtf(fsum);
 
        return TRUE;
    }
 
    BOOL GetStatic(CNoiseStat &out)
    {
        return GetStatic(out, GetAddress(), GetDataSize());
    }
    BOOL GetStatic(CNoiseStat & top, CNoiseStat &bottom)
    {
        int y= GetHeight()/2;
        int size= y*GetWidth();
 
        GetStatic(top, GetAddress(), size);
        GetStatic(bottom, GetData(0, y), GetDataSize()- size);
 
        return TRUE;
    }
};
 
#define MOSIS_MEMORY_ALIGN    16
template<typename Ty>
class CTightBuff// 4byte ¾ó¶óÀÎÀ» »ç¿ëÇÏÁö ¾Ê´Â ¹öÆÛ.
{
protected:
    Ty        *m_pData;
    int        m_Width;
    int        m_Height;
    int        m_DataSpace;
public:
    CTightBuff()
    {
        m_Width= 0;
        m_Height= 0;
        m_DataSpace= 0;
        m_pData= NULL;
    }
    virtual ~CTightBuff()
    {
        ReleaseSpace();
    }
    Ty        *GetDataAddress(){return m_pData;}
    Ty        *GetDataAddress(int x, int y){return m_pData+ x+ y*GetWidth();}
    int        GetWidth(){return m_Width;}
    int        GetHeight(){return m_Height;}
    int        GetDataSize(){return GetWidth()*GetHeight();}
    BOOL    IsValidBuffer(){return (m_pData != NULL) && (m_Width>0) && (m_Height>0);}
 
    void 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 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= (Ty*)_mm_malloc(space*sizeof(Ty), MOSIS_MEMORY_ALIGN);
#else
            m_pData= new Ty[space];
#endif
            m_DataSpace= space;
        }
 
        m_Width= width;
        m_Height= height;
        return IsValidBuffer();
    }
    int        GetIndex(int x, int y)    {return x+ y*GetWidth();}
    void SetPixel(int x, int y, Ty val)
    {
        *(m_pData+ x+ y*GetWidth())= val;
    }
 
public:
    BOOL MakeDiffBuff(CMosisBuffer &src, CMosisBuffer &cmp, SubMulti s1, SubMulti s2, BOOL bVert= FALSE)
    {
        SetSize(src.GetWidth(), src.GetHeight());
 
        BYTE *pSrc, *pCmp;
        Ty    *pDiff;
        int    pixelDist= 1;
 
        int x, y;
        SubMulti ssum;
        ssum= s1+ s2;
 
        ASSERT(ssum > 1);
        Ty rslt;
 
        if(bVert)
            pixelDist= cmp.GetDataWidth();
 
        for(y= 0; y< GetHeight(); y++)
        {
            x= 0;
            pSrc= src.GetDataAddress(x, y);
            pCmp= cmp.GetDataAddress(x, y);
            pDiff= GetDataAddress(x, y);
            for(; x< GetWidth(); x++)
            {
                rslt= *pSrc;
                rslt= rslt- (s1*(*pCmp)+ s2*(*(pCmp+pixelDist)))/ssum;
                *pDiff= rslt;
 
                pSrc++;
                pCmp++;
                pDiff++;
            }
        }
        return TRUE;
    }
 
    BOOL GetStatic(CNoiseStat &out, Ty* pData, int count)
    {
        INT64    sum= 0;
        int        realCount= 0;
        float noise= out.m_NoiseTh;
        Ty*        pDiff= pData;
 
        for(int x= 0; x< count; x++)
        {
            if(abs(*pDiff) > noise)
            {
                sum+= *pDiff;
                realCount++;
            }
            pDiff++;
        }
 
        if(realCount < 1)
        {
            out.Reset();
            return FALSE;
        }
 
        float mean= sum;
        out.m_Mean= mean= mean/realCount;
 
        float diff, fsum= 0;
        pDiff= pData;
        int rough, roughsum= 0;
        for(int x= 0; x< count; x++)
        {
            if(abs(*pDiff) > noise)
            {
                diff= *pDiff;
                diff-= mean;
                diff= diff*diff;
                fsum+= diff;
            }
            if(x > 0)
                roughsum+= abs(rough- *pDiff);
            rough= *pDiff;
            pDiff++;
        }
        if(count > 2)
            out.m_Rough= (float)roughsum/realCount;
        fsum/=realCount;
        out.m_StdDeviation= sqrtf(fsum);
 
        return TRUE;
    }
 
    BOOL GetStatic(CNoiseStat &out)
    {
        return GetStatic(out, GetDataAddress(), GetDataSize());
    }
    BOOL GetStatic(CNoiseStat & top, CNoiseStat &bottom)
    {
        int y= GetHeight()/2;
        int size= y*GetWidth();
 
        GetStatic(top, GetDataAddress(), size);
        GetStatic(bottom, GetDataAddress(0, y), GetDataSize()- size);
 
        return TRUE;
    }
};