Skip to content

Instantly share code, notes, and snippets.

@terotil
Last active December 23, 2015 08:48
Show Gist options
  • Save terotil/6609768 to your computer and use it in GitHub Desktop.
Save terotil/6609768 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Snippet to decode images hidden using method described
# in http://randomperspective.com/comic/93/
require 'rubygems'
require 'oily_png'
input_name = ARGV.first
# masks
LOW_RGB_BITS = 0b00000001_00000001_00000001_00000000
LOW_R_BIT = 0b00000001_00000000_00000000_00000000
ALL_R_BITS = 0b11111111_00000000_00000000_00000000
LOW_G_BIT = 0b00000000_00000001_00000000_00000000
ALL_G_BITS = 0b00000000_11111111_00000000_00000000
LOW_B_BIT = 0b00000000_00000000_00000001_00000000
ALL_B_BITS = 0b00000000_00000000_11111111_00000000
ALL_A_BITS = 0b00000000_00000000_00000000_11111111
ChunkyPNG::Image.from_file(input_name).tap do |input|
w, h = input.width / 3, input.height / 3
ChunkyPNG::Image.new(w, h, ChunkyPNG::Color::TRANSPARENT).tap do |output|
0.upto(w-1) do |x|
0.upto(h-1) do |y|
ix, iy = 3*x, 3*y
new_color = ALL_A_BITS # to create an opaque image
new_color |= ((input.get_pixel(ix+0, iy+0) & LOW_RGB_BITS) << 7)
new_color |= ((input.get_pixel(ix+1, iy+0) & LOW_RGB_BITS) << 6)
new_color |= ((input.get_pixel(ix+2, iy+0) & LOW_RGB_BITS) << 5)
new_color |= ((input.get_pixel(ix+0, iy+1) & LOW_RGB_BITS) << 4)
new_color |= ((input.get_pixel(ix+1, iy+1) & LOW_RGB_BITS) << 3)
new_color |= ((input.get_pixel(ix+2, iy+1) & LOW_RGB_BITS) << 2)
new_color |= ((input.get_pixel(ix+0, iy+2) & LOW_RGB_BITS) << 1)
new_color |= (input.get_pixel(ix+1, iy+2) & LOW_RGB_BITS)
# flip bits in color if "flip bit" is on
flip_color = input.get_pixel(ix+2, iy+2)
new_color ^= ALL_R_BITS if (flip_color & LOW_R_BIT) == LOW_R_BIT
new_color ^= ALL_G_BITS if (flip_color & LOW_G_BIT) == LOW_G_BIT
new_color ^= ALL_B_BITS if (flip_color & LOW_B_BIT) == LOW_B_BIT
output.set_pixel(x, y, new_color)
end
end
output.save("#{input_name}.extract.png", :fast_rgb)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment