-
Notifications
You must be signed in to change notification settings - Fork 34
/
png_transform.pl
158 lines (118 loc) · 3.39 KB
/
png_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
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
#!/usr/bin/perl
# Author: Trizen
# Date: 21 December 2023
# https://github.com/trizen
# Apply the reversible PNG transform on arbitrary data.
# The transformation can be made irreversible by lossy
# compressing the PNG file with a tool like "pngquant".
use 5.020;
use strict;
use warnings;
use GD qw();
use Getopt::Long qw(GetOptions);
use experimental qw(signatures);
GD::Image->trueColor(1);
binmode(STDIN, ':raw');
binmode(STDOUT, ':raw');
sub encode_data ($data) {
my @bytes = unpack("C*", $data);
my $c = 1 + int(scalar(@bytes) / 3);
my $width = int(sqrt($c));
my $height = int($c / $width) + 1;
say STDERR ":: File size: ", scalar(@bytes);
say STDERR ":: Image size: $width x $height";
my $image = GD::Image->new($width, $height)
or die "Can't create image";
my $size = scalar(@bytes);
OUTER: foreach my $y (0 .. $height - 1) {
foreach my $x (0 .. $width - 1) {
if ($size > 0) {
my $index = $image->colorResolve(shift(@bytes) // 0, shift(@bytes) // 0, shift(@bytes) // 0);
$image->setPixel($x, $y, $index);
$size -= 3;
}
else {
last OUTER;
}
}
}
return $image;
}
sub decode_data ($img_data, $length) {
my $image = GD::Image->new($img_data)
or die "Can't read image: $!";
my ($width, $height) = $image->getBounds();
my $data = '';
my $size = 0;
OUTER: foreach my $y (0 .. $height - 1) {
foreach my $x (0 .. $width - 1) {
my $index = $image->getPixel($x, $y);
if ($size < $length) {
my ($red, $green, $blue) = $image->rgb($index);
$data .= pack('C3', $red, $green, $blue);
$size += 3;
}
else {
last OUTER;
}
}
}
while (length($data) > $length) {
chop $data;
}
return $data;
}
my $compression = 9;
my $decode_size = undef;
sub help ($exit_code = 0) {
print <<"EOT";
usage: $0 [options] [input] [output]
options:
-d --decode=size : how many bytes to decode
example:
# Encode
perl $0 input.txt encoded.png
# Decode
perl $0 -d=size encoded.png decoded.txt
EOT
exit($exit_code);
}
GetOptions('d|decode=s' => \$decode_size,
"h|help" => sub { help(0) },)
or die("Error in command line arguments\n");
my $data_file = shift(@ARGV) // help(2);
my $output_file = shift(@ARGV);
my $data = do {
open my $fh, '<:raw', $data_file
or die "Can't open file <<$data_file>> for reading: $!";
local $/;
<$fh>;
};
if (defined($decode_size)) {
my $decoded = decode_data($data, $decode_size);
if (length($decoded) != $decode_size) {
warn sprintf("Incorrect size: len(T) = %d != len(D) = %d\n", length($decoded), length($data));
}
if (defined($output_file)) {
open my $fh, '>:raw', $output_file
or die "Can't open file <<$output_file>> for writing: $!";
print $fh $decoded;
close $fh;
}
else {
print $decoded;
}
}
else {
my $img = encode_data($data);
my $png = $img->png($compression);
if (defined($output_file)) {
open my $fh, '>:raw', $output_file
or die "Can't open file <<$output_file>> for writing: $!";
print $fh $png;
close $fh;
}
else {
print $png;
}
}