vertical_scrambler.pl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/perl
  2. # Author: Trizen
  3. # Date: 05 April 2024
  4. # https://github.com/trizen
  5. # Scramble the pixels in each column inside an image, using a deterministic method.
  6. use 5.036;
  7. use GD;
  8. use Getopt::Std qw(getopts);
  9. GD::Image->trueColor(1);
  10. sub scramble ($str) {
  11. my $i = length($str);
  12. $str =~ s/(.{$i})(.)/$2$1/gs while (--$i > 0);
  13. return $str;
  14. }
  15. sub unscramble ($str) {
  16. my $i = 0;
  17. my $l = length($str);
  18. $str =~ s/(.)(.{$i})/$2$1/gs while (++$i < $l);
  19. return $str;
  20. }
  21. sub scramble_image ($file, $function) {
  22. my $image = GD::Image->new($file) || die "Can't open file <<$file>>: $!";
  23. my ($width, $height) = $image->getBounds();
  24. my $new_image = GD::Image->new($width, $height);
  25. foreach my $x (0 .. $width - 1) {
  26. my (@R, @G, @B);
  27. foreach my $y (0 .. $height - 1) {
  28. my ($R, $G, $B) = $image->rgb($image->getPixel($x, $y));
  29. push @R, $R;
  30. push @G, $G;
  31. push @B, $B;
  32. }
  33. @R = unpack('C*', $function->(pack('C*', @R)));
  34. @G = unpack('C*', $function->(pack('C*', @G)));
  35. @B = unpack('C*', $function->(pack('C*', @B)));
  36. foreach my $y (0 .. $height - 1) {
  37. $new_image->setPixel($x, $y, $new_image->colorAllocate($R[$y], $G[$y], $B[$y]));
  38. }
  39. }
  40. return $new_image;
  41. }
  42. sub usage ($exit_code = 0) {
  43. print <<"EOT";
  44. usage: $0 [options] [input.png] [output.png]
  45. options:
  46. -d : decode the image
  47. -h : print this message and exit
  48. EOT
  49. exit($exit_code);
  50. }
  51. getopts('dh', \my %opts);
  52. my $input_file = $ARGV[0] // usage(2);
  53. my $output_file = $ARGV[1] // "output.png";
  54. if (not -f $input_file) {
  55. die "Input file <<$input_file>> does not exist!\n";
  56. }
  57. my $img = $opts{d} ? scramble_image($input_file, \&unscramble) : scramble_image($input_file, \&scramble);
  58. open(my $out_fh, '>:raw', $output_file) or die "can't create output file <<$output_file>>: $!";
  59. print $out_fh $img->png(9);
  60. close $out_fh;