File: filter.c

package info (click to toggle)
libepsilon 0.9.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,168 kB
  • ctags: 1,205
  • sloc: ansic: 11,329; sh: 9,321; perl: 936; xml: 86; makefile: 84
file content (460 lines) | stat: -rw-r--r-- 14,356 bytes parent folder | download | duplicates (6)
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
/*
 * $Id: filter.c,v 1.21 2010/04/05 06:11:55 simakov Exp $
 *
 * EPSILON - wavelet image compression library.
 * Copyright (C) 2006,2007,2010 Alexander Simakov, <xander@entropyware.info>
 *
 * This file is part of EPSILON
 *
 * EPSILON is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * EPSILON is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with EPSILON.  If not, see <http://www.gnu.org/licenses/>.
 *
 * http://epsilon-project.sourceforge.net
 */

#include <common.h>
#include <filter.h>
#include <filterbank.h>
#include <daub97lift.h>
#include <mem_alloc.h>
#include <string.h>

inline local int periodic_extension(int index, int length)
{
    if (index >= 0) {
        if (index < length) {
            return index;
        } else {
            return (index % length);
        }
    } else {
        return (length - 1 - (ABS(index) - 1) % length);
    }
}

inline local int symmetric_W_extension(int index, int length)
{
    if ((index >= 0) && (index < length)) {
        return index;
    }

    if (length == 1) {
        return 0;
    }

    index = ABS(index) % (2 * length - 2);

    if (index >= length) {
        index = 2 * length - 2 - index;
    }

    return index;
}

inline local int symmetric_H_extension(int index, int length)
{
    if ((index >= 0) && (index < length)) {
        return index;
    }

    if (length == 1) {
        return 0;
    }

    index = (ABS(index) - (index < 0)) % (2 * length);

    if (index >= length) {
        index = 2 * length - index - 1;
    }

    return index;
}

/* Note: output_length parameter is not actually used but it's preserved
 * in order to make downsample_signal() symmetric to and upsample_signal(). */
inline local void downsample_signal(coeff_t *input_signal, coeff_t *output_signal,
                                    int input_length, int output_length, int phase)
{
    int i, j;

    for (i = phase, j = 0; i < input_length; i += 2, j++) {
        output_signal[j] = input_signal[i];
    }
}

inline local void upsample_signal(coeff_t *input_signal, coeff_t *output_signal,
                                  int input_length, int output_length, int phase)
{
    int i, j, k;

    for (i = 0, j = phase; i < input_length; i++, j += 2) {
        output_signal[j] = input_signal[i];
    }

    for (k = phase ^ 1; k < output_length; k += 2) {
        output_signal[k] = 0;
    }
}

inline local void filter_periodic(coeff_t *input_signal, coeff_t *output_signal,
                                  int signal_length, filter_t *filter)
{
    int i, j, k;

    switch (filter->causality) {
        case CAUSAL:
        {
            for (i = 0; i < signal_length; i += 1) {
                output_signal[i] = 0;
                for (j = 0; j < filter->length; j++) {
                    k = periodic_extension(i - j, signal_length);
                    output_signal[i] += input_signal[k] * filter->coeffs[j];
                }
            }

            break;
        }
        case ANTICAUSAL:
        {
            for (i = 0; i < signal_length; i += 2) {
                output_signal[i] = 0;
                for (j = 0; j < filter->length; j++) {
                    k = periodic_extension(i + j, signal_length);
                    output_signal[i] +=
                        input_signal[k] * filter->coeffs[filter->length - j - 1];
                }
            }

            break;
        }
        default:
        {
            assert(0);
            break;
        }
    }
}

inline local void filter_symmetric(coeff_t *input_signal, coeff_t *output_signal,
                                   int signal_length, filter_t *filter)
{
    int i, j, k1, k2;

    switch (filter->causality) {
        case SYMMETRIC_WHOLE:
        {
            if (filter->type == LOWPASS_ANALYSIS) {
                for (i = 0; i < signal_length; i += 2) {
                    output_signal[i] = input_signal[i] * filter->coeffs[0];
                    for (j = 1; j < filter->length; j++) {
                        k1 = symmetric_W_extension(i + j, signal_length);
                        k2 = symmetric_W_extension(i - j, signal_length);
                        output_signal[i] +=
                            (input_signal[k1] + input_signal[k2]) * filter->coeffs[j];
                    }
                }
            } else if (filter->type == HIGHPASS_ANALYSIS) {
                for (i = 1; i < signal_length; i += 2) {
                    output_signal[i] = input_signal[i] * filter->coeffs[0];
                    for (j = 1; j < filter->length; j++) {
                        k1 = symmetric_W_extension(i + j, signal_length);
                        k2 = symmetric_W_extension(i - j, signal_length);
                        output_signal[i] +=
                            (input_signal[k1] + input_signal[k2]) * filter->coeffs[j];
                    }
                }
            } else {
                for (i = 0; i < signal_length; i++) {
                    output_signal[i] = input_signal[i] * filter->coeffs[0];
                    for (j = 1; j < filter->length; j++) {
                        k1 = symmetric_W_extension(i + j, signal_length);
                        k2 = symmetric_W_extension(i - j, signal_length);
                        output_signal[i] +=
                            (input_signal[k1] + input_signal[k2]) * filter->coeffs[j];
                    }
                }
            }

            break;
        }
        /* Some day I hope to add 'case SYMMETRIC_HALF' here */
        default:
        {
            assert(0);
            break;
        }
    }
}

local void analysis_1D(coeff_t *input_signal, coeff_t *output_signal,
                       coeff_t *temp, int signal_length, filterbank_t *fb)
{
    coeff_t *lowpass;
    coeff_t *highpass;

    /* Sanity checks */
    assert(signal_length > 0);
    assert((fb->type == BIORTHOGONAL) || ((fb->type == ORTHOGONAL)
            && !(signal_length & 1)));

    /* Trivial case */
    if (signal_length == 1) {
        output_signal[0] = input_signal[0] * SQRT2;
        return;
    }

    if (fb->type == ORTHOGONAL) {
        lowpass = output_signal;
        highpass = output_signal + signal_length / 2;

        /* Lowpass analysis */
        filter_periodic(input_signal, temp, signal_length, fb->lowpass_analysis);
        downsample_signal(temp, lowpass, signal_length, signal_length / 2, PHASE_EVEN);

        /* Highpass analysis */
        filter_periodic(input_signal, temp, signal_length, fb->highpass_analysis);
        downsample_signal(temp, highpass, signal_length, signal_length / 2, PHASE_EVEN);
    } else {
        lowpass = output_signal;
        highpass = output_signal + signal_length / 2 + (signal_length & 1);

        /* Lowpass analysis */
        filter_symmetric(input_signal, temp, signal_length, fb->lowpass_analysis);
        downsample_signal(temp, lowpass, signal_length, (signal_length + 1) / 2, PHASE_EVEN);

        /* Highpass analysis */
        filter_symmetric(input_signal, temp, signal_length, fb->highpass_analysis);
        downsample_signal(temp, highpass, signal_length, signal_length / 2, PHASE_ODD);
    }
}

local void synthesis_1D(coeff_t *input_signal, coeff_t *output_signal,
                        coeff_t *temp1, coeff_t *temp2, coeff_t *temp3,
                        int signal_length, filterbank_t *fb)
{
    coeff_t *lowpass;
    coeff_t *highpass;
    int i;

    /* Sanity checks */
    assert(signal_length > 0);
    assert((fb->type == BIORTHOGONAL) || ((fb->type == ORTHOGONAL)
            && !(signal_length & 1)));

    /* Trivial case */
    if (signal_length == 1) {
        output_signal[0] = input_signal[0] / SQRT2;
        return;
    }

    if (fb->type == ORTHOGONAL) {
        lowpass = input_signal;
        highpass = input_signal + signal_length / 2;

        /* Lowpass synthesis */
        upsample_signal(lowpass, temp1, signal_length / 2, signal_length, PHASE_EVEN);
        filter_periodic(temp1, temp2, signal_length, fb->lowpass_synthesis);

        /* Highpass synthesis */
        upsample_signal(highpass, temp1, signal_length / 2, signal_length, PHASE_EVEN);
        filter_periodic(temp1, temp3, signal_length, fb->highpass_synthesis);
    } else {
        lowpass = input_signal;
        highpass = input_signal + signal_length / 2 + (signal_length & 1);

        /* Lowpass synthesis */
        upsample_signal(lowpass, temp1, (signal_length + 1) / 2, signal_length, PHASE_EVEN);
        filter_symmetric(temp1, temp2, signal_length, fb->lowpass_synthesis);

        /* Highpass synthesis */
        upsample_signal(highpass, temp1, signal_length / 2, signal_length, PHASE_ODD);
        filter_symmetric(temp1, temp3, signal_length, fb->highpass_synthesis);
    }

    /* Combine arrays */
    for (i = 0; i < signal_length; i++) {
        output_signal[i] = temp2[i] + temp3[i];
    }
}

void analysis_2D(coeff_t **input_signal, coeff_t **output_signal,
                 int signal_length, int mode, filterbank_t *fb)
{
    coeff_t *input;
    coeff_t *output;
    coeff_t *temp;

    int scale, length;
    int scales;
    int i, j;

    assert(signal_length > 1);

    /* Transform as many times as possible */
    scales = number_of_bits(signal_length) - 1;

    /* Sanity checks */
    assert(((mode == MODE_NORMAL) && (signal_length == 1 << scales)) ||
           ((mode == MODE_OTLPF) && (signal_length == (1 << scales) + 1)));

    input = xmalloc(signal_length * sizeof(coeff_t));
    output = xmalloc(signal_length * sizeof(coeff_t));
    temp = xmalloc(signal_length * sizeof(coeff_t));

    for (i = 0; i < signal_length; i++) {
        for (j = 0; j < signal_length; j++) {
            output_signal[i][j] = input_signal[i][j];
        }
    }

    /* Transform image */
    for (scale = 0; scale < scales; scale++) {
        length = mode + (1 << (scales - scale));

        /* Transform rows */
        for (i = 0; i < length; i++) {
            for (j = 0; j < length; j++) {
                input[j] = output_signal[i][j];
            }

            if (!strcmp(fb->id, "daub97lift")) {
                if (length % 2) {
                    daub97lift_analysis_1D_odd(input, output, length);
                } else {
                    daub97lift_analysis_1D_even(input, output, length);
                }
            } else {
                analysis_1D(input, output, temp, length, fb);
            }

            for (j = 0; j < length; j++) {
                output_signal[i][j] = output[j];
            }
        }

        /* Transform columns */
        for (i = 0; i < length; i++) {
            for (j = 0; j < length; j++) {
                input[j] = output_signal[j][i];
            }

            if (!strcmp(fb->id, "daub97lift")) {
                if (length % 2) {
                    daub97lift_analysis_1D_odd(input, output, length);
                } else {
                    daub97lift_analysis_1D_even(input, output, length);
                }
            } else {
                analysis_1D(input, output, temp, length, fb);
            }

            for (j = 0; j < length; j++) {
                output_signal[j][i] = output[j];
            }
        }
    }

    free(input);
    free(output);
    free(temp);
}

void synthesis_2D(coeff_t **input_signal, coeff_t **output_signal,
                  int signal_length, int mode, filterbank_t *fb)
{
    coeff_t *input;
    coeff_t *output;
    coeff_t *temp1;
    coeff_t *temp2;
    coeff_t *temp3;

    int scale, length;
    int scales;
    int i, j;

    assert(signal_length > 1);

    /* Transform as many times as possible */
    scales = number_of_bits(signal_length) - 1;

    /* Sanity checks */
    assert(((mode == MODE_NORMAL) && (signal_length == 1 << scales)) ||
           ((mode == MODE_OTLPF) && (signal_length == (1 << scales) + 1)));

    /* Temporary arrays */
    input = xmalloc(signal_length * sizeof(coeff_t));
    output = xmalloc(signal_length * sizeof(coeff_t));
    temp1 = xmalloc(signal_length * sizeof(coeff_t));
    temp2 = xmalloc(signal_length * sizeof(coeff_t));
    temp3 = xmalloc(signal_length * sizeof(coeff_t));

    for (i = 0; i < signal_length; i++) {
        for (j = 0; j < signal_length; j++) {
            output_signal[i][j] = input_signal[i][j];
        }
    }

    /* Transform image */
    for (scale = 0; scale < scales; scale++) {
        length = mode + (1 << (scale + 1));

        /* Transform rows */
        for (i = 0; i < length; i++) {
            for (j = 0; j < length; j++) {
                input[j] = output_signal[i][j];
            }

            if (!strcmp(fb->id, "daub97lift")) {
                if (length % 2) {
                    daub97lift_synthesis_1D_odd(input, output, length);
                } else {
                    daub97lift_synthesis_1D_even(input, output, length);
                }
            } else {
                synthesis_1D(input, output, temp1, temp2, temp3, length, fb);
            }

            for (j = 0; j < length; j++) {
                output_signal[i][j] = output[j];
            }
        }

        /* Transform columns */
        for (i = 0; i < length; i++) {
            for (j = 0; j < length; j++) {
                input[j] = output_signal[j][i];
            }

            if (!strcmp(fb->id, "daub97lift")) {
                if (length % 2) {
                    daub97lift_synthesis_1D_odd(input, output, length);
                } else {
                    daub97lift_synthesis_1D_even(input, output, length);
                }
            } else {
                synthesis_1D(input, output, temp1, temp2, temp3, length, fb);
            }

            for (j = 0; j < length; j++) {
                output_signal[j][i] = output[j];
            }
        }
    }

    /* Release temporary arrays */
    free(input);
    free(output);
    free(temp1);
    free(temp2);
    free(temp3);
}