outguess-png.pl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!/usr/bin/perl
  2. # Author: Trizen
  3. # Date: 06 February 2022
  4. # Edit: 31 July 2022
  5. # https://github.com/trizen
  6. # Hide arbitrary data into the pixels of a PNG image, storing 3 bits in each pixel color.
  7. # Concept inspired by outguess:
  8. # https://github.com/resurrecting-open-source-projects/outguess
  9. # https://uncovering-cicada.fandom.com/wiki/OutGuess
  10. # Q: How does it work?
  11. # A: The script uses the GD library to read the RGB color values of each pixel.
  12. # Then it changes the last bit of each value to one bit from the data to be encoded.
  13. # Q: How does the decoding work?
  14. # A: The first 32 bits from the first 32 pixels of the image, form the length of the encoded data.
  15. # Then the remaining bits (3 bits from each pixel) are collected to form the encoded data.
  16. # The script also does transparent Deflate compression and decompression of the encoded data.
  17. use 5.020;
  18. use strict;
  19. use warnings;
  20. no warnings 'once';
  21. use GD qw();
  22. use Getopt::Long qw(GetOptions);
  23. use experimental qw(signatures);
  24. GD::Image->trueColor(1);
  25. binmode(STDIN, ':raw');
  26. binmode(STDOUT, ':raw');
  27. sub encode_data ($data, $img_file) {
  28. my $image = GD::Image->new($img_file)
  29. or die "Can't open image <<$img_file>>: $!";
  30. $image = $image->newFromJpegData($image->jpeg(100));
  31. require IO::Compress::RawDeflate;
  32. IO::Compress::RawDeflate::rawdeflate(\$data, \my $compressed_data)
  33. or die "rawdeflate failed: $IO::Compress::RawDeflate::RawDeflateError\n";
  34. $data = $compressed_data;
  35. my $bin = unpack("B*", $data);
  36. my ($width, $height) = $image->getBounds();
  37. my $maximum_data_size = 3 * (($width * $height - 32) >> 3);
  38. my $data_size = length($bin) >> 3;
  39. if ($data_size == 0) {
  40. die sprintf("No data was given!\n");
  41. }
  42. if ($data_size > $maximum_data_size) {
  43. die sprintf(
  44. "Data is too large (%s bytes) for this image (exceeded by %.2f%%).\n"
  45. . "Maximum data size for this image is %s bytes.\n",
  46. $data_size, 100 - ($maximum_data_size / $data_size * 100),
  47. $maximum_data_size
  48. );
  49. }
  50. warn sprintf("Compressed data size: %s bytes (%.2f%% out of max %s bytes)\n",
  51. $data_size, $data_size / $maximum_data_size * 100,
  52. $maximum_data_size);
  53. my $length_bin = unpack("B*", pack("N*", $data_size));
  54. $bin = reverse($length_bin . $bin);
  55. my $size = length($bin);
  56. OUTER: foreach my $y (0 .. $height - 1) {
  57. foreach my $x (0 .. $width - 1) {
  58. my $index = $image->getPixel($x, $y);
  59. if ($size > 0) {
  60. my ($red, $green, $blue) = $image->rgb($index);
  61. $index = $image->colorResolve(map { (($_ >> 1) << 1) | (chop($bin) || 0) } ($red, $green, $blue));
  62. $size -= 3;
  63. }
  64. else {
  65. last OUTER;
  66. }
  67. $image->setPixel($x, $y, $index);
  68. }
  69. }
  70. return $image;
  71. }
  72. sub decode_data ($img_file) {
  73. my $image = GD::Image->new($img_file)
  74. or die "Can't open image <<$img_file>>: $!";
  75. my ($width, $height) = $image->getBounds();
  76. my $bin = '';
  77. my $size = 0;
  78. my $length = $width * $height;
  79. my $find_length = 1;
  80. my $max_data_size = 3 * ($length - 4);
  81. OUTER: foreach my $y (0 .. $height - 1) {
  82. foreach my $x (0 .. $width - 1) {
  83. my $index = $image->getPixel($x, $y);
  84. if ($size < $length) {
  85. my ($red, $green, $blue) = $image->rgb($index);
  86. $bin .= join('', map { $_ & 1 } ($red, $green, $blue));
  87. $size += 3;
  88. if ($find_length and $size >= 32) {
  89. $length = unpack("N*", pack("B*", substr($bin, 0, 32)));
  90. $find_length = 0;
  91. $size = length($bin) - 32;
  92. $bin = substr($bin, 32);
  93. if ($length > $max_data_size or $length == 0) {
  94. die "No hidden data was found in this image!\n";
  95. }
  96. warn sprintf("Compressed data size: %s bytes\n", $length);
  97. $length <<= 3;
  98. }
  99. }
  100. else {
  101. last OUTER;
  102. }
  103. }
  104. }
  105. my $data = pack("B*", substr($bin, 0, $length));
  106. require IO::Uncompress::RawInflate;
  107. IO::Uncompress::RawInflate::rawinflate(\$data, \my $uncompressed)
  108. or die "rawinflate failed: $IO::Uncompress::RawInflate::RawInflateError\n";
  109. warn sprintf("Uncompressed data size: %s bytes\n", length($uncompressed));
  110. return $uncompressed;
  111. }
  112. sub help ($exit_code = 0) {
  113. print <<"EOT";
  114. usage: $0 [options] [input] [output]
  115. options:
  116. -z [file] : encode a given data file
  117. example:
  118. # Encode
  119. perl $0 -z=data.txt input.jpg encoded.png
  120. # Decode
  121. perl $0 encoded.png decoded-data.txt
  122. EOT
  123. exit($exit_code);
  124. }
  125. my $data_file;
  126. GetOptions("z|f|encode=s" => \$data_file,
  127. "h|help" => sub { help(0) },)
  128. or die("Error in command line arguments\n");
  129. if (defined($data_file)) {
  130. my $input_image = shift(@ARGV) // help(2);
  131. my $output_image = shift(@ARGV);
  132. open my $fh, '<:raw', $data_file
  133. or die "Can't open file <<$data_file>> for reading: $!";
  134. my $data = do {
  135. local $/;
  136. <$fh>;
  137. };
  138. close $fh;
  139. my $img = encode_data($data, $input_image);
  140. if (defined($output_image)) {
  141. if ($output_image !~ /\.png\z/i) {
  142. die "The output image must have the '.png' extension!\n";
  143. }
  144. open my $fh, '>:raw', $output_image
  145. or die "Can't open file <<$output_image>> for writing: $!";
  146. print $fh $img->png(9);
  147. close $fh;
  148. }
  149. else {
  150. print $img->png(9);
  151. }
  152. }
  153. else {
  154. my $input_image = shift(@ARGV) // help(2);
  155. my $output_file = shift(@ARGV);
  156. my $data = decode_data($input_image);
  157. if (defined($output_file)) {
  158. open my $fh, '>:raw', $output_file
  159. or die "Can't open file <<$output_file>> for writing: $!";
  160. print $fh $data;
  161. close $fh;
  162. }
  163. else {
  164. print $data;
  165. }
  166. }