Apple_Generator/apple_st75256.rb

35 lines
774 B
Ruby

require 'bundler'
Bundler::require
require 'rmagick'
include Magick
IMAGE_DIR='video/256_128'
OUTPUT_NAME='video/256_128/apple_st75256.bin'
COLOR_STEP=(Magick::QuantumRange + 1) / 4
def convert_image(filename)
im = Image.read(filename)
pix_ary = Array.new(8192, 0)
im[0].each_pixel do |pix, col, row|
color = pix.intensity / COLOR_STEP
byte_offset = 256 * (row / 4) + col
bit_offset = (3 - (row % 4)) * 2
pix_ary[byte_offset] &= ~(3 << bit_offset)
pix_ary[byte_offset] |= (color << bit_offset)
end
pix_ary
end
File.open(OUTPUT_NAME, "w+") do |f|
Dir.glob("#{IMAGE_DIR}/*.bmp").each_with_index do |img, i|
puts "#{i} image processed."
f.write(convert_image(img).pack("C*"))
end
end