mrDarker
2025-08-13 6ba20a51d17d6be2cf9886f3622863e95c2994fb
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// MatchBuffer.cpp: implementation of the CMatchBuffer class.
//
//////////////////////////////////////////////////////////////////////
 
#include "stdafx.h"
#include "MatchBuffer.h"
#include "SISBuffer.h"
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
 
#define MOSIS_MEMORY_ALIGN 16
 
#if defined(MOSIS_MEMORY_ALIGN)
#include "malloc.h"
#endif
 
CUShortBuff::CUShortBuff()
: m_pData(NULL), m_Width(0), m_Height(0), m_DataSpace(0)
{}
CUShortBuff::~CUShortBuff()
{
    ReleaseSpace();
}
void CUShortBuff::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 CUShortBuff::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= (ushort*)_mm_malloc(space*sizeof(ushort), MOSIS_MEMORY_ALIGN);
#else
        m_pData= new ushort[space];
#endif
        m_DataSpace= space;
    }
    m_Width= width;
    m_Height= height;
 
    return IsValidBuffer();
}
 
 
BOOL CUShortBuff::SetBuffPyramid(CSISBuffer fromBuff, CRect roi, int multi)
{
    int width, height;
    width= (int)(roi.Width()/multi);///8*8;
    height= roi.Height()/multi;
 
    ushort data;
 
    if(! SetSize(width, height))
        return FALSE;
 
    int x, y;
    int divide= multi*multi;
//    divide= 1;
 
    for(y= 0; y< height; y++)
    {
        for(x= 0; x< width; x++)
        {
            data= 0;
            for(int yy= y*multi; yy< y*multi+ multi; yy++)
            {
                for(int xx= x*multi; xx< x*multi+ multi; xx++)
                {
                    data+= fromBuff.GetPixel(roi.left+ xx, roi.top+ yy);
                }
            }
            SetPixel(x, y, (ushort)((float)data/divide+ 0.5));
//             for(int xx= x*multi; xx< x*multi+ multi; xx++)
//             {
//                 data+= fromBuff.GetPixel(roi.left+ xx, roi.top+ xx);
//             }
//             SetPixel(x, y, data/multi);
        }
    }
/*
    __m128i    mmResult, mmResult1, mmResult2;
    __m128i    mmA, mmB;
    __m128i    mmAA, mmBB;
    __m128i mmZeroData= _mm_setzero_si128();
    BYTE *pData;
    multi= 2;
    int        distance= 2<<multi;
    for(y= 0; y< height; y++)
    {
        for(x= 0; x< width- distance; x+=distance)
        {
            mmResult= _mm_setzero_si128();
            for(int i= 0; i< distance; i++)// yÃàÀ¸·Î ÇÕ.
            {
                pData= fromBuff.GetDataAddress(x*distance, y*distance+ i);
                mmA= _mm_loadu_si128((__m128i*) (pData));
                mmB= _mm_loadu_si128((__m128i*) (pData+ distance*8));
                
                mmAA= _mm_unpackhi_epi8(mmA, mmZeroData);
                mmBB= _mm_unpacklo_epi8(mmA, mmZeroData);
                
                mmResult1= _mm_hadd_epi16(mmAA, mmBB);
                
                mmAA= _mm_unpackhi_epi8(mmB, mmZeroData);
                mmBB= _mm_unpacklo_epi8(mmB, mmZeroData);
                
                mmResult2= _mm_hadd_epi16(mmAA, mmBB);
                
                mmResult2= _mm_hadd_epi16(mmResult1, mmResult2);
 
                mmResult= _mm_add_epi16(mmResult, mmResult2);
            }
            _mm_storeu_si128((__m128i*)GetDataAddress(x, y), mmResult);
        }
    }
*/
    return TRUE;
}
 
BOOL    CUShortBuff::CopyFrom(CUShortBuff &fromBuffer, CRect rect)
{
    if(rect.left < 0 || rect.right > fromBuffer.GetWidth() ||
        rect.top < 0 || rect.bottom > fromBuffer.GetHeight())
        return FALSE;
    if(! IsValidBuffer())
        return FALSE;
 
    if(GetHeight() < rect.Height())
        return FALSE;
 
    for(int i= 0; i< rect.Height(); i++)
    {
        memcpy(GetDataAddress(0, i), fromBuffer.GetDataAddress(rect.left, i+ rect.top), GetWidth()*sizeof(ushort));
    }
    return TRUE;
}
 
 
void CByteBuff::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 CByteBuff::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= (BYTE*)_mm_malloc(space*sizeof(BYTE), MOSIS_MEMORY_ALIGN);
#else
        m_pData= new BYTE[space];
#endif
        m_DataSpace= space;
    }
    
    m_Width= width;
    m_Height= height;
    return IsValidBuffer();
}
BOOL CByteBuff::CopyFrom(CSISBuffer &fromBuffer, CRect rect)
{
    // ¹öÆÛ Ã¼Å©¸¦ ÇÏÁö ¾Ê°Ú´Ù... ¶óÀο¡¼­´Â ¹öÆÛ°¡ ºÙ¾î ÀÖÀ¸´Ï±î..
    // ±Û¶ó½º TILT¿¡ ÀÇÇØ Á¡ÁøÀû ¸ÅĪ¿¡¼­ ¹öÆÛ °æ°è¸¦ ÃʰúÇÒ ¼ö ÀÖ´Ù.
//     if(rect.left < 0 || rect.right > fromBuffer.GetWidth() ||
//         rect.top < 0 || rect.bottom > fromBuffer.GetHeight())
//         return FALSE;
    if(! IsValidBuffer())
        return FALSE;
    
    if(GetHeight() < rect.Height())
        return FALSE;
 
    int width= rect.Width();
    if(GetWidth() < width)
        width= GetWidth();
    if(rect.left < 0)
        return FALSE;
    if(rect.top < 0)
        return FALSE;
    if(rect.right > fromBuffer.GetWidth())
        return FALSE;
    if(rect.bottom > fromBuffer.GetHeight())
        return FALSE;
    
    for(int i= 0; i< rect.Height(); i++)
    {
        memcpy(GetDataAddress(0, i), fromBuffer.GetDataAddress(rect.left, i+ rect.top), width);
    }
    return TRUE;
/*    int width, height;
    width= rect.Width();
    height= rect.Height();
    if(SetSize(rect.Width(), rect.Height()))
    {
        for(int yy= 0; yy< rect.Height(); yy++)
        {
            memcpy(GetDataAddress(0, yy), fromBuffer.GetDataAddress(rect.left, rect.top+ yy), rect.Width());
        }
    }*/
}
BOOL CByteBuff::CopyFrom(CByteBuff &fromBuffer, CRect rect)
{
    if(rect.left < 0 || rect.right > fromBuffer.GetWidth() ||
        rect.top < 0 || rect.bottom > fromBuffer.GetHeight())
        return FALSE;
    if(! IsValidBuffer())
        return FALSE;
    
    if(GetHeight() < rect.Height())
        return FALSE;
    
    int width= rect.Width();
    if(GetWidth() < width)
        width= GetWidth();
    if(rect.left < 0)
        return FALSE;
    if(rect.top < 0)
        return FALSE;
    if(rect.right > fromBuffer.GetWidth())
        return FALSE;
    if(rect.bottom > fromBuffer.GetHeight())
        return FALSE;
 
    for(int i= 0; i< rect.Height(); i++)
    {
        memcpy(GetDataAddress(0, i), fromBuffer.GetDataAddress(rect.left, i+ rect.top), width);
    }
    return TRUE;
/*    if(SetSize(rect.Width(), rect.Height()))
    {
        for(int yy= 0; yy< rect.Height(); yy++)
        {
            memcpy(GetDataAddress(0, yy), fromBuffer.GetDataAddress(rect.left, rect.top+ yy), rect.Width());
        }
    }*/
}
 
BOOL CByteBuff::SetBuffPyramid(CSISBuffer fromBuff, CRect roi, int multi)
{
    int width, height;
    width= (int)(roi.Width()/multi);///8*8;
    height= roi.Height()/multi;
 
    ushort data;
 
    if(! SetSize(width, height))
        return FALSE;
 
    int x, y;
    int divide= multi*multi;
 
    for(y= 0; y< height; y++)
    {
        for(x= 0; x< width; x++)
        {
            data= 0;
            for(int yy= y*multi; yy< y*multi+ multi; yy++)
            {
                for(int xx= x*multi; xx< x*multi+ multi; xx++)
                {
                    data+= fromBuff.GetPixel(roi.left+ xx, roi.top+ yy);
                }
            }
            SetPixel(x, y, (BYTE)((float)data/divide+ 0.5));
//             for(int xx= x*multi; xx< x*multi+ multi; xx++)
//             {
//                 data+= fromBuff.GetPixel(roi.left+ xx, roi.top+ xx);
//             }
//             SetPixel(x, y, data/multi);
        }
    }
/*
    __m128i    mmResult, mmResult1, mmResult2;
    __m128i    mmA, mmB;
    __m128i    mmAA, mmBB;
    __m128i mmZeroData= _mm_setzero_si128();
    BYTE *pData;
    multi= 2;
    int        distance= 2<<multi;
    for(y= 0; y< height; y++)
    {
        for(x= 0; x< width- distance; x+=distance)
        {
            mmResult= _mm_setzero_si128();
            for(int i= 0; i< distance; i++)// yÃàÀ¸·Î ÇÕ.
            {
                pData= fromBuff.GetDataAddress(x*distance, y*distance+ i);
                mmA= _mm_loadu_si128((__m128i*) (pData));
                mmB= _mm_loadu_si128((__m128i*) (pData+ distance*8));
                
                mmAA= _mm_unpackhi_epi8(mmA, mmZeroData);
                mmBB= _mm_unpacklo_epi8(mmA, mmZeroData);
                
                mmResult1= _mm_hadd_epi16(mmAA, mmBB);
                
                mmAA= _mm_unpackhi_epi8(mmB, mmZeroData);
                mmBB= _mm_unpacklo_epi8(mmB, mmZeroData);
                
                mmResult2= _mm_hadd_epi16(mmAA, mmBB);
                
                mmResult2= _mm_hadd_epi16(mmResult1, mmResult2);
 
                mmResult= _mm_add_epi16(mmResult, mmResult2);
            }
            _mm_storeu_si128((__m128i*)GetDataAddress(x, y), mmResult);
        }
    }
*/
    return TRUE;
}