bwt_rgb_horizontal_transform.pl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/perl
  2. # Author: Trizen
  3. # Date: 05 April 2024
  4. # Edit: 09 April 2024
  5. # https://github.com/trizen
  6. # Apply the Burrows-Wheeler transform on each row (RGB-wise) of an image.
  7. use 5.036;
  8. use GD;
  9. use Getopt::Std qw(getopts);
  10. use Compression::Util qw(bwt_encode bwt_decode);
  11. GD::Image->trueColor(1);
  12. sub apply_bwt ($file) {
  13. my $image = GD::Image->new($file) || die "Can't open file <<$file>>: $!";
  14. my ($width, $height) = $image->getBounds();
  15. my $new_image = GD::Image->new($width + 3, $height);
  16. foreach my $y (0 .. $height - 1) {
  17. my (@R, @G, @B);
  18. foreach my $x (0 .. $width - 1) {
  19. my ($R, $G, $B) = $image->rgb($image->getPixel($x, $y));
  20. push @R, $R;
  21. push @G, $G;
  22. push @B, $B;
  23. }
  24. my ($R, $R_idx) = bwt_encode(pack('C*', @R));
  25. my ($G, $G_idx) = bwt_encode(pack('C*', @G));
  26. my ($B, $B_idx) = bwt_encode(pack('C*', @B));
  27. @R = unpack('C*', $R);
  28. @G = unpack('C*', $G);
  29. @B = unpack('C*', $B);
  30. $new_image->setPixel(0, $y, $R_idx);
  31. $new_image->setPixel(1, $y, $G_idx);
  32. $new_image->setPixel(2, $y, $B_idx);
  33. foreach my $x (0 .. $width - 1) {
  34. $new_image->setPixel($x + 3, $y, $new_image->colorAllocate($R[$x], $G[$x], $B[$x]));
  35. }
  36. }
  37. return $new_image;
  38. }
  39. sub undo_bwt ($file) {
  40. my $image = GD::Image->new($file) || die "Can't open file <<$file>>: $!";
  41. my ($width, $height) = $image->getBounds();
  42. my $new_image = GD::Image->new($width - 3, $height);
  43. foreach my $y (0 .. $height - 1) {
  44. my (@R, @G, @B);
  45. my $R_idx = $image->getPixel(0, $y);
  46. my $G_idx = $image->getPixel(1, $y);
  47. my $B_idx = $image->getPixel(2, $y);
  48. foreach my $x (3 .. $width - 1) {
  49. my ($R, $G, $B) = $image->rgb($image->getPixel($x, $y));
  50. push @R, $R;
  51. push @G, $G;
  52. push @B, $B;
  53. }
  54. @R = unpack 'C*', bwt_decode(pack('C*', @R), $R_idx);
  55. @G = unpack 'C*', bwt_decode(pack('C*', @G), $G_idx);
  56. @B = unpack 'C*', bwt_decode(pack('C*', @B), $B_idx);
  57. foreach my $x (0 .. $width - 3 - 1) {
  58. $new_image->setPixel($x, $y, $new_image->colorAllocate($R[$x], $G[$x], $B[$x]));
  59. }
  60. }
  61. return $new_image;
  62. }
  63. sub usage ($exit_code = 0) {
  64. print <<"EOT";
  65. usage: $0 [options] [input.png] [output.png]
  66. options:
  67. -d : decode the image
  68. -h : print this message and exit
  69. EOT
  70. exit($exit_code);
  71. }
  72. getopts('dh', \my %opts);
  73. my $input_file = $ARGV[0] // usage(2);
  74. my $output_file = $ARGV[1] // "output.png";
  75. if (not -f $input_file) {
  76. die "Input file <<$input_file>> does not exist!\n";
  77. }
  78. my $img = $opts{d} ? undo_bwt($input_file) : apply_bwt($input_file);
  79. open(my $out_fh, '>:raw', $output_file) or die "can't create output file <<$output_file>>: $!";
  80. print $out_fh $img->png(9);
  81. close $out_fh;