SDC C-Project CF Review 프로그램
LYW
2021-09-23 c77236e1522caa3e2082dd7b5b32f6c30b125172
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
 
#include "stdafx.h"
#include "basefwt.h"
#include "vec1d.h"
 
///////////////////////////////////constructors/destructors///////////////////////////////////////////////////////////////////
//constructor status 0-not initilized, 1,2-ok after init(), -1,-2,... errors
BaseFWT2D::BaseFWT2D(const wchar_t* fname) : m_status(0),
                tH(0), tG(0), H(0), G(0), H2m(0), G2m(0), H2m1(0), G2m1(0),
                J(0), TH(0), m_width(0), m_height(0),
                spec(0), tspec(0), spec2d(0), tspec2d(0)
{
    wcscpy(filter, fname);
 
    FILE* flt = _wfopen(fname, L"rt");
    if (flt) 
    {
        if ((tH = loadfilter(flt)) == 0) 
        {
            m_status = -2;
            return;
        }
 
        if ((tG = loadfilter(flt)) == 0) 
        {
            m_status = -3;
            return;
        }
 
        if ((H = loadfilter(flt)) == 0) 
        {
            m_status = -4;
            return;
        }
 
        if ((G = loadfilter(flt)) == 0) 
        {
            m_status = -5;
            return;
        }
 
        fclose(flt);
 
        makeHGsynth();
    } 
    else
    {
        m_status = -1;    //filter file failed to open
    }
}
 
BaseFWT2D::BaseFWT2D(const wchar_t* fname,
                     const float* tH, unsigned int thL, int thZ,
                     const float* tG, unsigned int tgL, int tgZ,
                     const float* H, unsigned int hL, int hZ,
                     const float* G, unsigned int gL, int gZ) : m_status(0),
                tH(0), tG(0), H(0), G(0), H2m(0), G2m(0), H2m1(0), G2m1(0),
                J(0), TH(0), m_width(0), m_height(0),
                spec(0), tspec(0), spec2d(0), tspec2d(0)
{
    wcscpy(filter, fname);
 
    this->tH = new vec1D(thL, -thZ, tH);
    this->tG = new vec1D(tgL, -tgZ, tG);
    this->H = new vec1D(hL, -hZ, H);
    this->G = new vec1D(gL, -gZ, G);
 
    makeHGsynth();
}
 
BaseFWT2D::~BaseFWT2D()
{
    if (tH) delete tH;
    if (tG) delete tG;
    if (H) delete H;
    if (G) delete G;
    if (H2m) delete H2m;
    if (G2m) delete G2m;
    if (H2m1) delete H2m1;
    if (G2m1) delete G2m1;
    close();                //close spec,tspec,spec2d,tspec2d buffers
}
///////////////////////////////////constructors/destructors///////////////////////////////////////////////////////////////////
 
 
 
//////////////////////////////////init/status functions//////////////////////////////////////////////////////////////////////
const wchar_t* BaseFWT2D::status(int& status) const
{
    status = m_status;
 
    switch (m_status) 
    {
 
    default:
 
    case 0:
            return 0;
 
    case 1:
    case 2:
            return L"ready for transforms";
 
    case -1:
            return L"failed to open filter file";
 
    case -2:
            return L"failed to load tH filter";
 
    case -3:
            return L"failed to load tG filter";
 
    case -4:
            return L"failed to load H filter";
 
    case -5:
            return L"failed to load G filter";
 
    }
}
 
vec1D* BaseFWT2D::loadfilter(FILE* flt) const
{
    int L, Z;
    if (fwscanf(flt, L"%d %d", &L, &Z) != 2)
        return 0;
 
    vec1D* wf = new vec1D(L, -Z);
 
    for (int i = wf->first(); i <= wf->last(); i++) 
    {
        float tmp;
        if (fwscanf(flt, L"%*d %f", &tmp) != 1)
            return 0;
        else
            (*wf)(i) = tmp;
    }
 
    return wf;
}
 
void BaseFWT2D::makeHGsynth()
{
    int size2m, offset2m;
    int size2m1, offset2m1;
    
    size2m = 0;
    size2m1 = 0;
    //arrange H2m,H2m1
    for (int m = H->first(); m <= H->last(); m++) //count how many odd even coeffs
    {      
        if (m % 2)
            size2m1++;
        else
            size2m++;
    }
    offset2m = (H->first() - (H->first() % 2)) / 2;
    offset2m1 = (H->first() + (H->first() % 2)) / 2;
 
    H2m = new vec1D(size2m, offset2m);
    H2m1 = new vec1D(size2m1, offset2m1);
 
    for (int m = H2m->first(); m <= H2m->last(); m++)
        (*H2m)(m) = (*H)(2 * m);
 
    for (int m = H2m1->first(); m <= H2m1->last(); m++)
        (*H2m1)(m) = (*H)(2 * m + 1);
 
    size2m = 0;
    size2m1 = 0;
 
    //arrange G2m,G2m1
    for (int m = G->first(); m <= G->last(); m++) 
    {
        if (m % 2)
            size2m1++;
        else
            size2m++;
    }
    offset2m = (G->first() - (G->first() % 2)) / 2;
    offset2m1 = (G->first() + (G->first() % 2)) / 2;
 
    G2m = new vec1D(size2m, offset2m);
    G2m1 = new vec1D(size2m1, offset2m1);
 
    for (int m = G2m->first(); m <= G2m->last(); m++)
        (*G2m)(m) = (*G)(2 * m);
 
    for (int m = G2m1->first(); m <= G2m1->last(); m++)
        (*G2m1)(m) = (*G)(2 * m + 1);
 
}
 
void BaseFWT2D::tracefilters(const wchar_t* fname) const
{
    FILE* fp = _wfopen(fname, L"wt");
    if (fp) {
        fwprintf(fp, L"tH\n");
        for (int i = tH->first(); i <= tH->last(); i++)
            fwprintf(fp, L" %2d  %g\n", i, (*tH)(i));
        fwprintf(fp, L"\ntG\n");
 
        for (int i = tG->first(); i <= tG->last(); i++)
            fwprintf(fp, L" %2d  %g\n", i, (*tG)(i));
        fwprintf(fp, L"\nH\n");
 
        for (int i = H->first(); i <= H->last(); i++)
            fwprintf(fp, L" %2d  %g\n", i, (*H)(i));
        fwprintf(fp, L"\nG\n");
 
        for (int i = G->first(); i <= G->last(); i++)
            fwprintf(fp, L" %2d  %g\n", i, (*G)(i));
        fwprintf(fp, L"\n\nH2m\n");
 
        for (int i = H2m->first(); i <= H2m->last(); i++)
            fwprintf(fp, L" %2d  %g\n", 2*i, (*H2m)(i));
        fwprintf(fp, L"\nH2m1\n");
 
        for (int i = H2m1->first(); i <= H2m1->last(); i++)
            fwprintf(fp, L" %2d  %g\n", 2*i + 1, (*H2m1)(i));
        fwprintf(fp, L"\nG2m\n");
 
        for (int i = G2m->first(); i <= G2m->last(); i++)
            fwprintf(fp, L" %2d  %g\n", 2*i, (*G2m)(i));
        fwprintf(fp, L"\nG2m1\n");
 
        for (int i = G2m1->first(); i <= G2m1->last(); i++)
            fwprintf(fp, L" %2d  %g\n", 2*i + 1, (*G2m1)(i));
 
        fclose(fp);
    }
}
 
void BaseFWT2D::init(unsigned int width, unsigned int height)
{
    if (width != m_width || height != m_height) 
    {
        close();
        m_width = width;
        m_height = height;
 
        spec = (char*)malloc(2 * width * height * sizeof(char));
        tspec = spec + width * height;
 
        spec2d = (char**)malloc(2 * height * sizeof(char*));     //setup rows
 
        for (unsigned int j = 0; j < 2*height; j++)
            spec2d[j] = &spec[j*width];   //setup cols
 
        tspec2d = &spec2d[height];
 
        m_status = 1;
    }
}
 
void BaseFWT2D::init(char* data, char* tdata, unsigned int width, unsigned int height)
{
    close();
    m_width = width;
    m_height = height;
 
    spec = data;          //data - tdata  might be not continuous in memory
    tspec = tdata;
 
    spec2d = (char**)malloc(2 * height * sizeof(char *));     //setup rows
 
    for (unsigned int j = 0; j < height; j++)
        spec2d[j] = &spec[j*width];     //setup cols
 
    for (unsigned int j = 0; j < height; j++)
        spec2d[j+height] = &tspec[j*width];   //setup cols
 
    tspec2d = &spec2d[height];
 
    m_status = 2;
}
 
void BaseFWT2D::close(void)
{
    m_width = 0;
    m_height = 0;
    if (m_status == 1) 
    {
        if (spec != 0) 
        {
            free(spec);
            spec = 0;
        }
    }
    if (spec2d != 0) 
    {
        free(spec2d);
        spec2d = 0;
    }
    m_status = 0;
}
//////////////////////////////////init/status functions//////////////////////////////////////////////////////////////////////
 
 
 
///////////////////////////////////////////////transforms/////////////////////////////////////////////////////////////////////
int BaseFWT2D::trans(unsigned int scales, unsigned int th)
{
    if (m_status <= 0)
            return -1;
 
    J = scales;
    TH = th;
    unsigned int w = m_width;
    unsigned int h = m_height;
 
    for (unsigned int j = 0; j < J; j++) 
    {
        transrows(tspec2d, spec2d, w, h);
        transcols(spec2d, tspec2d, w, h);
        w /= 2;
        h /= 2;
        TH /= 4;
    }
 
    return 0;
}
 
int BaseFWT2D::trans(const char* data, unsigned int scales, unsigned int th)
{
    if (m_status <= 0)
        return -1;
 
    mmxmemcpy(spec, data, m_width*m_height);    //copy data to spec
    return trans(scales, th);     //fwt transform
}
 
int BaseFWT2D::trans(const unsigned char* data, unsigned int scales, unsigned int th)
{
    if (m_status <= 0)
        return -1;
 
    sub128(spec, data, m_width*m_height);    //copy data to spec and correct DC
    return trans(scales, th);    //fwt transform
}
///////////////////////////////////////////////transforms/////////////////////////////////////////////////////////////////////
 
 
 
//////////////////////////////////////////////synths//////////////////////////////////////////////////////////////////////////
int BaseFWT2D::synth()
{
    if (m_status <= 0)
        return -1;
 
    unsigned int w = m_width / (unsigned int)pow(2.0f, (float)J);
    unsigned int h = m_height / (unsigned int)pow(2.0f, (float)J);
 
    while (J) 
    {
        synthcols(tspec2d, spec2d, w, h);
        synthrows(spec2d, tspec2d, w, h);
        w *= 2;
        h *= 2;
        J--;
    }
 
    return 0;
}
 
int BaseFWT2D::synth(char* data)
{
    int res;
    res = synth();
    
    if (res != 0)
        return res;
 
    mmxmemcpy(data, spec, m_width*m_height);      //copy restored spec to data
    
    return res;
}
 
int BaseFWT2D::synth(unsigned char* data)
{
    int res;
    res = synth();
    
    if (res != 0)
        return res;
 
    add128(data, spec, m_width*m_height);     //copy restored spec to data and correct DC
 
    return res;
}
//////////////////////////////////////////////synths//////////////////////////////////////////////////////////////////////////
 
 
//////////////////////////////////////////////denoising///////////////////////////////////////////////////////////////////////
void BaseFWT2D::remove_LLband()
{
    if (m_status <= 0)
        return;
 
    unsigned int width = m_width / (unsigned int)(pow(2.0f, (float)getJ()));
    unsigned int height = m_height / (unsigned int)(pow(2.0f, (float)getJ()));
        
    for (unsigned int y = 0; y < height; y++) 
        for (unsigned int x = 0; x < width; x++)                         
            spec2d[y][x] = 0;                
 
}
//////////////////////////////////////////////denoising///////////////////////////////////////////////////////////////////////
 
//////////////////////////mmx routines////////////////////////////////////////////////////////////////////////////////////////
void BaseFWT2D::mmxmemcpy(char* dest, const char* sour, unsigned int size)
{
    //for(int i=0; i<m_width*m_height; i++)  //stub
    //dest[i] = sour[i];
    unsigned int modsize8 = size % 8;
    __m64* pdest = (__m64*)dest;
    const __m64* psour = (__m64*)sour;
 
    for (unsigned int i = 0; i < size / 8; i++)
        *pdest++ = *psour++;
 
    if (modsize8) 
    {
        dest = (char*)pdest;
        sour = (char*)psour;
        for (unsigned int i = 0; i < modsize8; i++)
            *dest++ = *sour++;
    }
 
    _mm_empty();
}
 
void BaseFWT2D::sub128(char* dest, const unsigned char* sour, unsigned int size)
{
    //for(int i=0; i<m_width*m_height; i++)
    // dest[i] = (char)((int)sour[i]-128);
 
    unsigned int modsize8 = size % 8;
    __m64 m128;
    m128.m64_u64 = 0x8080808080808080;
    __m64* pdest = (__m64*)dest;
    const __m64* psour = (__m64*)sour;
 
    for (unsigned int i = 0; i < size / 8; i++)
        *pdest++ = _mm_sub_pi8(*psour++ , m128);
 
    if (modsize8) 
    {
        dest = (char*)pdest;
        sour = (unsigned char*)psour;
        for (unsigned int i = 0; i < modsize8; i++)
            *dest++ = (char)((int) * sour++ - 128);
    }
 
    _mm_empty();
}
void BaseFWT2D::add128(unsigned char* dest, const char* sour, unsigned int size)
{
    //for(int i=0; i<m_width*m_height; i++)
    // data[i] = (unsigned char)((int)spec[i]+128);
 
    unsigned int modsize8 = size % 8;
    __m64 m128;
    m128.m64_u64 = 0x8080808080808080;
    __m64* pdest = (__m64*)dest;
    __m64* psour = (__m64*)sour;
 
    for (unsigned int i = 0; i < size / 8; i++)
        *pdest++ = _mm_add_pi8(*psour++ , m128);
 
    if (modsize8) 
    {
        dest = (unsigned char*)pdest;
        sour = (char*)psour;
        for (unsigned int i = 0; i < modsize8; i++)
            *dest++ = (unsigned char)((int) * sour++ + 128);
    }
 
    _mm_empty();
}