forked from trizen/perl-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jpeg_transform.pl
145 lines (109 loc) · 3.2 KB
/
jpeg_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
#!/usr/bin/perl
# Author: Trizen
# Date: 21 December 2023
# https://github.com/trizen
# Apply the irreversible JPEG transform on arbitrary data. (lossy transform)
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 ":: 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 $quality = 100;
my $save_image = undef;
sub help ($exit_code = 0) {
print <<"EOT";
usage: $0 [options] [input] [output]
options:
-q --quality=i : quality level in range 0-100 (default: $quality)
-s --save-image=s : save the JPEG image to a given filename
example:
perl $0 input.txt transformed.txt
EOT
exit($exit_code);
}
GetOptions(
's|save-image=s' => \$save_image,
'q|quality=i' => \$quality,
"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>;
};
my $img = encode_data($data);
my $jpeg = $img->jpeg($quality);
if (defined($save_image)) {
open my $fh, '>:raw', $save_image
or die "Can't open file <<$save_image>> for writing: $!";
print $fh $jpeg;
close $fh;
}
my $transformed = decode_data($jpeg, length($data));
if (length($transformed) != length($data)) {
die sprintf("Failed: len(T) = %d != len(D) = %d\n", length($transformed), length($data));
}
if (defined($output_file)) {
open my $fh, '>:raw', $output_file
or die "Can't open file <<$output_file>> for writing: $!";
print $fh $transformed;
close $fh;
}
else {
print $transformed;
}