Created
January 15, 2013 03:14
-
-
Save sczizzo/4535739 to your computer and use it in GitHub Desktop.
Convert a gzipped LBM file into a simple GIF without animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# Offset | Length | Description | |
# ======================================== | |
# 0 | 1 | minor width offset | |
# 1 | 1 | major width offset | |
# 2 | 1 | minor height offset | |
# 3 | 1 | major height offset | |
# 4 | 768 | 256-color pallet | |
# 772 | 1 | number of cycles (N) | |
# 773 | 6N | cycle information | |
# 773+6N | W*H | image information | |
# | |
require 'zlib' | |
require 'rmagick' | |
include Magick | |
exit(-1) if ARGV.length != 1 | |
# Load raw drawing information | |
drawing = { | |
width: 0, | |
height: 0, | |
ncycles: 0, | |
cycles: [], | |
pallet: [], | |
data: [] | |
} | |
File.open(ARGV.first, 'rb') do |f| | |
img = Zlib::GzipReader.new(f) | |
drawing[:width] = img.getbyte + 256 * img.getbyte | |
drawing[:height] = img.getbyte + 256 * img.getbyte | |
256.times do | |
drawing[:pallet] << { | |
red: img.getbyte, | |
green: img.getbyte, | |
blue: img.getbyte | |
} | |
end | |
drawing[:ncycles] = img.getbyte | |
drawing[:ncycles].times do | |
drawing[:cycles] << { | |
reversed: img.getbyte + 256 * img.getbyte, | |
rate: img.getbyte + 256 * img.getbyte, | |
low: img.getbyte, | |
high: img.getbyte | |
} | |
end | |
(drawing[:width] * drawing[:height]).times do | |
drawing[:data] << img.getbyte | |
end | |
end | |
# Create a blank image for the drawing | |
img = Image.new(drawing[:width], drawing[:height]) do | |
self.depth = 8 | |
end | |
# Recreate the drawing pixel-by-pixel | |
drawing[:height].times do |y| | |
drawing[:width].times do |x| | |
# Look up the pixel color in our pallet | |
off = y * drawing[:width] + x | |
pidx = drawing[:data][off] | |
color = drawing[:pallet][pidx] | |
# Convert the 8-bit colors to 16-bit | |
red = 257 * color[:red] | |
green = 257 * color[:green] | |
blue = 257 * color[:blue] | |
img.store_pixels(x, y, 1, 1, [Pixel.new(red, green, blue, QuantumRange)]) | |
end | |
end | |
img.write('out.gif') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment