Advertisement
t_a_w

convert_vic2_screensh

Nov 2nd, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.89 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require "pathname"
  4. require "tempfile"
  5.  
  6. def strip_alpha!(input_path, output_path)
  7.   bmp = File.open(input_path, "rb").read
  8.   header = bmp[0, 54]
  9.   image = bmp[54..-3]
  10.   final = bmp[-2..-1]
  11.   raise unless image.size % 4 # WTF last 2 bytes
  12.  
  13.   image_out = ""
  14.   (image.size/4).times do |i|
  15.     r,g,b,a = image[i*4, 4].unpack("CCCC")
  16.     image_out << [r,g,b,255].pack("CCCC")
  17.   end
  18.   open(output_path, "wb") do |fh|
  19.     fh.write header
  20.     fh.write image_out
  21.     fh.write final
  22.   end
  23. end
  24.  
  25. def convert_screenshot!(input_path)
  26.   output_path = Pathname(File.basename(input_path, ".bmp") + ".png")
  27.   input_path = Pathname(input_path)
  28.   return if output_path.exist?
  29.  
  30.   mid_file = Tempfile.new(['foo', '.bmp'])
  31.   strip_alpha!(input_path, mid_file.path)
  32.   system "convert", mid_file.path, output_path.to_s
  33. end
  34.  
  35. ARGV.each do |path|
  36.   convert_screenshot!(path)
  37. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement