Skip to content

Instantly share code, notes, and snippets.

@noniq
Created February 24, 2013 15:01
Show Gist options
  • Save noniq/5024140 to your computer and use it in GitHub Desktop.
Save noniq/5024140 to your computer and use it in GitHub Desktop.
Convert print dumpfile from VICE C64 emulator to a real image.
#!/usr/bin/env ruby
require "logger"
require "rmagick"
class Input
attr_reader :filename
def initialize(filename)
@filename = filename
end
def max_line_length
lines.map(&:length).max
end
def lines
@lines ||= File.readlines(filename)
end
end
class Output
BG_PIXEL = Magick::Pixel.new(Magick::QuantumRange, Magick::QuantumRange, Magick::QuantumRange)
FG_PIXEL = Magick::Pixel.new(0, 0, 0)
attr_reader :filename, :input, :img, :log
def initialize(filename, input, log)
@filename = filename
@input = input
@log = log
end
def convert
log.debug "Converting #{width}x#{height} image:"
input.lines.each_with_index do |line, i|
log.debug("Line #{i} ...") if i % 1000 == 0
convert_line i, line
end
log.debug "Saving image."
save_image
end
def width
@width ||= input.max_line_length
end
def height
@height ||= input.lines.count
end
def img
@img ||= Magick::Image.new(width, height) do |img|
img.background_color = BG_PIXEL
end
end
def convert_line(y, data)
pixels = data.each_char.map do |c|
c == " " ? BG_PIXEL : FG_PIXEL
end
img.store_pixels(0, y, pixels.size, 1, pixels)
end
def save_image
img.write(filename)
end
end
def usage
puts "Usage: #{$0} INPUTFILE OUTPUTFILE"
exit
end
input_file = ARGV[0] || usage
output_file = ARGV[1] || usage
logger = Logger.new(STDERR)
logger.formatter = ->(severity, datetime, progname, msg){ "#{msg}\n" }
input = Input.new(input_file)
output = Output.new(output_file, input, logger)
output.convert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment