Advertisement
Guest User

extractChannelMSB.rb

a guest
Dec 1st, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.57 KB | None | 0 0
  1. image = ChunkyPNG::Image.from_file('source.png')
  2.  
  3. bytes = Array.new(0)
  4. bytes = []
  5. done = ""
  6. image.to_alpha_channel_stream.split("").each { |byte| bytes << byte.to_s.unpack('B*')[0][0] }
  7. (0..image.to_alpha_channel_stream.length/8).each {|n| bits = bytes.shift(8); done << bits.join.to_i(2);  }
  8. File.open("alpha_bitplane.bin", 'wb') { |file| file.write(done) }
  9.  
  10. puts "Wrote #{done.length} bytes:  alpha_bitplane.bin"
  11.  
  12. bytes = []
  13. done = ""
  14. red = image.to_rgb_stream.unpack('axx'*(image.to_rgb_stream.length/3)).join
  15. red.split("").each { |byte| bytes << byte.to_s.unpack('B*')[0][0] }
  16. (0..image.to_rgb_stream.length/24).each {|n| bits = bytes.shift(8); done << bits.join.to_i(2);  }
  17. File.open("red_bitplane.bin", 'wb') { |file| file.write(done) }
  18.  
  19. puts "Wrote #{done.length} bytes:  red_bitplane.bin"
  20.  
  21. bytes = []
  22. done = ""
  23. green = image.to_rgb_stream.unpack('xax'*(image.to_rgb_stream.length/3)).join
  24. green.split("").each { |byte| bytes << byte.to_s.unpack('B*')[0][0] }
  25. (0..image.to_rgb_stream.length/24).each {|n| bits = bytes.shift(8); done << bits.join.to_i(2);  }
  26. File.open("green_bitplane.bin", 'wb') { |file| file.write(done) }
  27.  
  28. puts "Wrote #{done.length} bytes:  green_bitplane.bin"
  29.  
  30. bytes = []
  31. done = ""
  32. blue = image.to_rgb_stream.unpack('xxa'*(image.to_rgb_stream.length/3)).join
  33. blue.split("").each { |byte| bytes << byte.to_s.unpack('B*')[0][0] }
  34. (0..image.to_rgb_stream.length/24).each {|n| bits = bytes.shift(8); done << bits.join.to_i(2);  }
  35. File.open("blue_bitplane.bin", 'wb') { |file| file.write(done) }
  36.  
  37. puts "Wrote #{done.length} bytes:  blue_bitplane.bin"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement