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
|
#!/usr/bin/perl -w
#
# $Id: make_filterbank.pl,v 1.10 2010/02/05 23:50:23 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
#
#
# This program makes C-structures from XML-based filter specifications.
# Usage example: make_filterbank *.filters
#
use strict;
use warnings;
use XML::Simple;
sub Main {
unless (@ARGV) {
print "Usage: make_filterbank.pl <files>\n";
exit(1);
}
# Process all files
for my $file (@ARGV) {
my $filter = parse_xml_file($file);
check_filter($filter);
make_filterbank($filter);
}
}
sub parse_xml_file {
my $file = shift @_;
print STDERR "Parsing file $file...\n";
my $xml = XML::Simple->new();
my $filter = $xml->XMLin($file);
return $filter;
}
sub check_filter {
my $filter = shift @_;
# Check filter id
unless (defined($filter->{'id'})) {
print STDERR "Filter id not specified.\n";
exit(1);
}
unless ($filter->{'id'} =~ /^[a-zA-Z_]\w*$/) {
print STDERR "Filter id must be alphanumerical and start with letter.\n";
exit(1);
}
# Check filter name
unless (defined($filter->{'name'})) {
print STDERR "Filter name not specified.\n";
exit(1);
}
# Check filter type
unless (defined($filter->{'type'})) {
print STDERR "Filter type not specified.\n";
exit(1);
}
if ((lc($filter->{'type'}) ne 'orthogonal') and
(lc($filter->{'type'}) ne 'biorthogonal'))
{
print STDERR "Filter type must be either orthogonal or biorthogonal.\n";
exit(1);
}
# Make pretty comment
if (defined($filter->{'info'})) {
my @strings = split(/\r?\n/, $filter->{'info'});
my $info;
STRING: for my $s (@strings) {
$s =~ s/^\s+//g;
$s =~ s/\s+$//g;
next STRING unless($s);
unless ($info) {
$info = "/* $s\n";
next STRING;
}
$info .= " * $s\n";
}
chomp($info);
$info .= " */\n";
$filter->{'info'} = $info;
}
# Check primary filter
unless (defined($filter->{'primary'}->{'content'})) {
print STDERR "Primary filter not specified.\n";
exit(1);
}
unless (defined($filter->{'primary'}->{'causality'})) {
print STDERR "Primary filter causality not specified.\n";
exit(1);
}
if ((lc($filter->{'primary'}->{'causality'}) ne 'causal') and
(lc($filter->{'primary'}->{'causality'}) ne 'anticausal') and
(lc($filter->{'primary'}->{'causality'}) ne 'symmetric_whole') and
(lc($filter->{'primary'}->{'causality'}) ne 'symmetric_half'))
{
print STDERR "Acceptable values for filter causality: ".
"causal, anticausal, symmetric_whole and symmetric_half.\n";
exit(1);
}
if ((lc($filter->{'type'}) eq 'orthogonal') and
(lc($filter->{'primary'}->{'causality'}) ne 'anticausal'))
{
print STDERR "Orthogonal filter must be anticausal.\n";
exit(1);
}
if ((lc($filter->{'type'}) eq 'biorthogonal') and
(lc($filter->{'primary'}->{'causality'}) !~ '^symmetric'))
{
print STDERR "Biorthogonal filter must be either ".
"symmetric_whole or symmetric_half.\n";
exit(1);
}
# Even-length biorthogonal filters are not supported yet.
if (lc($filter->{'primary'}->{'causality'}) eq 'symmetric_half') {
print STDERR "Biorthogonal symmetric_half filters are not supported yet.\n";
exit(1);
}
my $primary_coeffs = $filter->{'primary'}->{'content'};
$primary_coeffs =~ s/^\s+//g;
$primary_coeffs =~ s/\s+$//g;
$primary_coeffs =~ s/\s+/ /g;
$filter->{'primary'}->{'content'} = $primary_coeffs;
# For biorthogonal filters check the dual filter as well
if (lc($filter->{'type'}) eq 'biorthogonal') {
unless (defined($filter->{'dual'}->{'content'})) {
print STDERR "Dual filter not specified.\n";
exit(1);
}
unless (defined($filter->{'dual'}->{'causality'})) {
print STDERR "Dual filter causality not specified.\n";
exit(1);
}
if (lc($filter->{'primary'}->{'causality'}) ne lc($filter->{'dual'}->{'causality'})) {
print STDERR "Causality of primary and dual filters must be the same.\n";
exit(1);
}
my $dual_coeffs = $filter->{'dual'}->{'content'};
$dual_coeffs =~ s/^\s+//g;
$dual_coeffs =~ s/\s+$//g;
$dual_coeffs =~ s/\s+/ /g;
$filter->{'dual'}->{'content'} = $dual_coeffs;
}
}
sub even_alternate_sign {
my @coeffs = @_;
# Alternate sign starting from coeff[0]
for (my $i = 0; $i < @coeffs; $i += 2) {
if ($coeffs[$i] < 0) {
$coeffs[$i] =~ s/^\-//;
} else {
$coeffs[$i] = '-'.$coeffs[$i];
}
}
return @coeffs;
}
sub odd_alternate_sign {
my @coeffs = @_;
# Alternate sign starting from coeff[1]
for (my $i = 1; $i < @coeffs; $i += 2) {
if ($coeffs[$i] < 0) {
$coeffs[$i] =~ s/^\-//;
} else {
$coeffs[$i] = '-'.$coeffs[$i];
}
}
return @coeffs;
}
sub make_filterbank {
my $filter = shift @_;
my @lowpass_analysis_coeffs;
my $lowpass_analysis_length;
my $lowpass_analysis_causality;
my @highpass_analysis_coeffs;
my $highpass_analysis_length;
my $highpass_analysis_causality;
my @lowpass_synthesis_coeffs;
my $lowpass_synthesis_length;
my $lowpass_synthesis_causality;
my @highpass_synthesis_coeffs;
my $highpass_synthesis_length;
my $highpass_synthesis_causality;
if (lc($filter->{'type'}) eq 'biorthogonal') {
# Make lowpass analysis filter
@lowpass_analysis_coeffs = split(/ /, $filter->{'primary'}->{'content'});
$lowpass_analysis_length = @lowpass_analysis_coeffs;
$lowpass_analysis_causality = uc($filter->{'primary'}->{'causality'});
# Make lowpass synthesis filter
@lowpass_synthesis_coeffs = split(/ /, $filter->{'dual'}->{'content'});
$lowpass_synthesis_length = @lowpass_synthesis_coeffs;
$lowpass_synthesis_causality = uc($filter->{'dual'}->{'causality'});
# Compute real filter length
my $analysis_length =
$lowpass_analysis_length * 2 -
($lowpass_analysis_causality eq 'SYMMETRIC_WHOLE');
my $synthesis_length =
$lowpass_synthesis_length * 2 -
($lowpass_synthesis_causality eq 'SYMMETRIC_WHOLE');
# Make highpass analysis filter
if (int($synthesis_length / 2) % 2) {
@highpass_analysis_coeffs =
even_alternate_sign(@lowpass_synthesis_coeffs);
} else {
@highpass_analysis_coeffs =
odd_alternate_sign(@lowpass_synthesis_coeffs);
}
$highpass_analysis_length = @highpass_analysis_coeffs;
$highpass_analysis_causality = $lowpass_analysis_causality;
# Make highpass synthesis filter
if (int($analysis_length / 2) % 2) {
@highpass_synthesis_coeffs =
odd_alternate_sign(@lowpass_analysis_coeffs);
} else {
@highpass_synthesis_coeffs =
even_alternate_sign(@lowpass_analysis_coeffs);
}
$highpass_synthesis_length = @highpass_synthesis_coeffs;
$highpass_synthesis_causality = $lowpass_analysis_causality;
} else {
# Make lowpass analysis filter
@lowpass_analysis_coeffs = split(/ /, $filter->{'primary'}->{'content'});
$lowpass_analysis_length = @lowpass_analysis_coeffs;
$lowpass_analysis_causality = 'ANTICAUSAL';
# Make lowpass synthesis filter
@lowpass_synthesis_coeffs = reverse(@lowpass_analysis_coeffs);
$lowpass_synthesis_length = $lowpass_analysis_length;
$lowpass_synthesis_causality = 'CAUSAL';
# Make highpass analysis filter
@highpass_analysis_coeffs = odd_alternate_sign(@lowpass_synthesis_coeffs);
$highpass_analysis_length = $lowpass_analysis_length;
$highpass_analysis_causality = 'ANTICAUSAL';
# Make highpass synthesis filter
@highpass_synthesis_coeffs = even_alternate_sign(@lowpass_analysis_coeffs);
$highpass_synthesis_length = $lowpass_analysis_length;
$highpass_synthesis_causality = 'CAUSAL';
}
# Print comment
print "$filter->{'info'}\n";
# Print lowpass analysis filter coeffs
print "static coeff_t $filter->{'id'}_lowpass_analysis_coeffs\[\] = {\n";
for (@lowpass_analysis_coeffs) {
$_ < 0 ? print " $_,\n" : print " $_,\n";
}
print "};\n\n";
# Print highpass analysis filter coeffs
print "static coeff_t $filter->{'id'}_highpass_analysis_coeffs\[\] = {\n";
for (@highpass_analysis_coeffs) {
$_ < 0 ? print " $_,\n" : print " $_,\n";
}
print "};\n\n";
# Print lowpass synthesis filter coeffs
print "static coeff_t $filter->{'id'}_lowpass_synthesis_coeffs\[\] = {\n";
for (@lowpass_synthesis_coeffs) {
$_ < 0 ? print " $_,\n" : print " $_,\n";
}
print "};\n\n";
# Print highpass synthesis filter coeffs
print "static coeff_t $filter->{'id'}_highpass_synthesis_coeffs\[\] = {\n";
for (@highpass_synthesis_coeffs) {
$_ < 0 ? print " $_,\n" : print " $_,\n";
}
print "};\n\n";
# Print lowpass analysis filter
print "static filter_t $filter->{'id'}_lowpass_analysis = {\n";
print " $lowpass_analysis_length,\n";
print " $lowpass_analysis_causality,\n";
print " LOWPASS_ANALYSIS,\n";
print " $filter->{'id'}_lowpass_analysis_coeffs,\n";
print "};\n\n";
# Print highpass analysis filter
print "static filter_t $filter->{'id'}_highpass_analysis = {\n";
print " $highpass_analysis_length,\n";
print " $highpass_analysis_causality,\n";
print " HIGHPASS_ANALYSIS,\n";
print " $filter->{'id'}_highpass_analysis_coeffs,\n";
print "};\n\n";
# Print lowpass synthesis filter
print "static filter_t $filter->{'id'}_lowpass_synthesis = {\n";
print " $lowpass_synthesis_length,\n";
print " $lowpass_synthesis_causality,\n";
print " LOWPASS_SYNTHESIS,\n";
print " $filter->{'id'}_lowpass_synthesis_coeffs,\n";
print "};\n\n";
# Print highpass synthesis filter
print "static filter_t $filter->{'id'}_highpass_synthesis = {\n";
print " $highpass_synthesis_length,\n";
print " $highpass_synthesis_causality,\n";
print " HIGHPASS_SYNTHESIS,\n";
print " $filter->{'id'}_highpass_synthesis_coeffs,\n";
print "};\n\n";
$filter->{'type'} = uc($filter->{'type'});
# And finally, print filterbank
print "static filterbank_t $filter->{'id'} = {\n";
print " \"$filter->{'id'}\",\n";
print " \"$filter->{'name'}\",\n";
print " $filter->{'type'},\n";
print " \&$filter->{'id'}_lowpass_analysis,\n";
print " \&$filter->{'id'}_highpass_analysis,\n";
print " \&$filter->{'id'}_lowpass_synthesis,\n";
print " \&$filter->{'id'}_highpass_synthesis,\n";
print "};\n\n";
}
Main();
|