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
//20161221 µ¥¸ð¿ëÀ¸·Î ±ÞÇϰԠ¸¸µë
//ÀÌÁøÈ­ °Ë»ç(¿§ÁöÅ©·¢°Ë»ç µî¿¡ ¾²ÀÚ)
#include "stdafx.h"
 
#include "BlobStorage.h"
#include <wmmintrin.h>
 
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
 
#include "SISAssem.h"
#include "AssemParam.h"
 
int CSISAssem::BinalizeInspection(BYTE *pSrc, int nThresS, int nThresE, CRect rtROI
                                    ,int nWidth, CDefectPair *pDefectdPair, int nStartPair, int nMaxPair
                                    ,int &nEndLine)
{    
    int nPair = nStartPair;    
    CDefectPair* PairAddr = pDefectdPair + nPair;
 
    if(nThresE < nThresS)
    {
        int nTemp = nThresS;
        nThresS = nThresE;
        nThresE = nTemp;
    }
 
    __m128i ZeroData = _mm_setzero_si128();
    __m128i ThresHigh = _mm_set1_epi8(UCHAR(nThresE)^0x80);
    __m128i ThresLow = _mm_set1_epi8(UCHAR(nThresS)^0x80);
    __m128i OneAll = _mm_set1_epi8(-1);
    __m128i Image;
    __m128i Result;
    __m128i Temp;    
    __m128i _x80 = _mm_set1_epi8('\x80');
 
    BYTE *pS, *pS_;
 
    int nx, ny;
    int nStartX, nEndX, nStartY, nEndY;
    nStartY = rtROI.top;
    nEndY = nStartY + rtROI.Height();
    nStartX = rtROI.left;
    nEndX = rtROI.right-16;
 
    pS = pSrc + nWidth * nStartY + nStartX;
    
    for(ny = nStartY; ny < nEndY; ny++)
    {        
        pS_ = pS;
        for(nx = nStartX; nx < nEndX; nx+=16)    //16°³¾¿ Ã³¸®
        {
            //À̹ÌÁöµ¥ÀÌÅÍ ·Îµù(src)
            Image = _mm_loadu_si128((__m128i*) (pS_));
 
            //Å«°Å            
            Temp = _mm_cmpgt_epi8(_mm_xor_si128(Image, _x80), ThresLow);
            Result = _mm_cmplt_epi8(_mm_xor_si128(Image, _x80), ThresHigh);
            Result = _mm_and_si128(Result, Temp);
 
            if(_mm_movemask_epi8(Result) != 0)
            {
                for(int i = 0; i < 16; i++)
                {
                    if(Result.m128i_i8[i] != 0)
                    {
                        PairAddr->s_DefectPos = DEFPOS_CENTER;
                        PairAddr->s_DefectPair = DEFPAIR_PPAIR;
                        PairAddr->s_nDefectX = nx + i;
                        PairAddr->s_nDefectY = ny;
                        PairAddr->s_DefectType = DEFTYPE_BLACK;
                        PairAddr->s_nGraySrc = Image.m128i_u8[i];
                        PairAddr->s_nGrayRef = nThresE;
                        PairAddr->s_nZone = 0;
                        PairAddr->s_nThresold = nThresE;
                        PairAddr++;
                        nPair++;
 
                        if(nPair >= nMaxPair)
                        {
                            nEndLine = ny;
                            return nPair;
                        }
                    }
                }            
            }
            pS_ += 16;
        }
        pS += nWidth;
    }
 
    /*
    nEndX+=16;
    for(ny = nStartY; ny < nEndY; ny++)
    {        
        pS_ = pS;
        for(; nx < nEndX; nx++)    //16°³¾¿ Ã³¸®
        {
            if(*(pS_) > nThresS &&  *(pS_) < nThresE)
            {
                PairAddr->s_DefectPos = DEFPOS_CENTER;
                PairAddr->s_DefectPair = DEFPAIR_PPAIR;
                PairAddr->s_nDefectX = nx;
                PairAddr->s_nDefectY = ny;
                PairAddr->s_DefectType = DEFTYPE_BLACK;
                PairAddr->s_nGraySrc = *(pS_);
                PairAddr->s_nGrayRef = nThresE;
                PairAddr->s_nZone = 0;
                PairAddr->s_nThresold = nThresE;
                PairAddr++;
                nPair++;
 
                if(nPair >= nMaxPair)
                {
                    nEndLine = ny;
                    return nPair;
                }
            }
 
            pS_ ++;
        }
        pS += nWidth;
    }
    */
 
    nEndLine = (int)rtROI.bottom;
    return nPair;
}