Skip to content

Instantly share code, notes, and snippets.

@ucnv
Created July 21, 2010 16:26
Show Gist options
  • Select an option

  • Save ucnv/484718 to your computer and use it in GitHub Desktop.

Select an option

Save ucnv/484718 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Test script for yet another JPEG glitch technique.
# Rewrite YCbCr sampling factors in JPEG header.
# It's a version of http://gist.github.com/167060
require 'erb'
data = File.open(ARGV.first) {|f| f.read }
data.force_encoding Encoding::BINARY
sof0 = data.index "\xFF\xC0"
ns = data[sof0 + 9].unpack('C').first
files = (4 ** ns).times.collect do |i|
sign = "%0#{2*ns}b" % i
ns.times do |j|
flag1 = (sign[j * 2] == "1") ? 0x20 : 0x10
flag2 = (sign[j * 2 + 1] == "1") ? 0x02 : 0x01
data[sof0 + 11 + j * 3] = [flag1 | flag2].pack('C')
end
file = sign + '.jpg'
File.open(file, 'w') do |f|
f.print data
end
file
end
file = 'index.html'
File.open(file, 'w') do |f|
f.print ERB.new(DATA.read).result(binding)
end
#`open index.html`
__END__
<html>
<head><title>Rewrite SOF0 Glitch</title></head>
<body>
<% files.each do |i| %>
<img alt="<%= i %>" src="<%= i %>">
<% end %>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment