-
Notifications
You must be signed in to change notification settings - Fork 33
/
bwt_rgb_vertical_transform.pl
115 lines (81 loc) · 2.88 KB
/
bwt_rgb_vertical_transform.pl
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
#!/usr/bin/perl
# Author: Trizen
# Date: 05 April 2024
# Edit: 09 April 2024
# https://github.com/trizen
# Apply the Burrows-Wheeler transform on each column (RGB-wise) of an image.
use 5.036;
use GD;
use Getopt::Std qw(getopts);
use Compression::Util qw(bwt_encode bwt_decode);
GD::Image->trueColor(1);
sub apply_bwt ($file) {
my $image = GD::Image->new($file) || die "Can't open file <<$file>>: $!";
my ($width, $height) = $image->getBounds();
my $new_image = GD::Image->new($width, $height + 3);
foreach my $x (0 .. $width - 1) {
my (@R, @G, @B);
foreach my $y (0 .. $height - 1) {
my ($R, $G, $B) = $image->rgb($image->getPixel($x, $y));
push @R, $R;
push @G, $G;
push @B, $B;
}
my ($R, $R_idx) = bwt_encode(pack('C*', @R));
my ($G, $G_idx) = bwt_encode(pack('C*', @G));
my ($B, $B_idx) = bwt_encode(pack('C*', @B));
@R = unpack('C*', $R);
@G = unpack('C*', $G);
@B = unpack('C*', $B);
$new_image->setPixel($x, 0, $R_idx);
$new_image->setPixel($x, 1, $G_idx);
$new_image->setPixel($x, 2, $B_idx);
foreach my $y (0 .. $height - 1) {
$new_image->setPixel($x, $y + 3, $new_image->colorAllocate($R[$y], $G[$y], $B[$y]));
}
}
return $new_image;
}
sub undo_bwt ($file) {
my $image = GD::Image->new($file) || die "Can't open file <<$file>>: $!";
my ($width, $height) = $image->getBounds();
my $new_image = GD::Image->new($width, $height - 3);
foreach my $x (0 .. $width - 1) {
my (@R, @G, @B);
my $R_idx = $image->getPixel($x, 0);
my $G_idx = $image->getPixel($x, 1);
my $B_idx = $image->getPixel($x, 2);
foreach my $y (3 .. $height - 1) {
my ($R, $G, $B) = $image->rgb($image->getPixel($x, $y));
push @R, $R;
push @G, $G;
push @B, $B;
}
@R = unpack 'C*', bwt_decode(pack('C*', @R), $R_idx);
@G = unpack 'C*', bwt_decode(pack('C*', @G), $G_idx);
@B = unpack 'C*', bwt_decode(pack('C*', @B), $B_idx);
foreach my $y (0 .. $height - 3 - 1) {
$new_image->setPixel($x, $y, $new_image->colorAllocate($R[$y], $G[$y], $B[$y]));
}
}
return $new_image;
}
sub usage ($exit_code = 0) {
print <<"EOT";
usage: $0 [options] [input.png] [output.png]
options:
-d : decode the image
-h : print this message and exit
EOT
exit($exit_code);
}
getopts('dh', \my %opts);
my $input_file = $ARGV[0] // usage(2);
my $output_file = $ARGV[1] // "output.png";
if (not -f $input_file) {
die "Input file <<$input_file>> does not exist!\n";
}
my $img = $opts{d} ? undo_bwt($input_file) : apply_bwt($input_file);
open(my $out_fh, '>:raw', $output_file) or die "can't create output file <<$output_file>>: $!";
print $out_fh $img->png(9);
close $out_fh;