Skip to content

Instantly share code, notes, and snippets.

@supechicken
Created August 19, 2023 06:19
Show Gist options
  • Save supechicken/d8facd15b3edb08600284a559acdee84 to your computer and use it in GitHub Desktop.
Save supechicken/d8facd15b3edb08600284a559acdee84 to your computer and use it in GitHub Desktop.
My ani2ico implementation written in Ruby
#!/usr/bin/env ruby
MetaStruct = {
file_magic: :txt, # File magic, should be "RIFF"
payload_size: :int, # Size of the file (excluding the file magic)
riff_chunkType: :txt, # Should be "ACON"
riff_chunkID: :txt, # Should be "anih"
riff_chunkSize: :int, # Should be 36
anih_chunkSize: :int, # same as riff_chunkSize
anih_numFrames: :int, # number of .ico images saved
anih_numSteps: :int,
anih_width: :int,
anih_height: :int,
anih_bitCount: :int,
anih_numPlanes: :int,
anih_displayRate: :int,
anih_flags: :int,
listChunkID: :txt, # Should be "LIST"
listChunkSize: :int, # Size of all .ico images plus RIFF subchunk headers
listHeaderID: :txt # Should be fram
}
ParsedMeta = {}
Filename = File.basename(ARGV[0], '.ani')
ANI_fileIO = File.open(ARGV[0], 'r')
warn "File infomation:\n\n"
MetaStruct.each_pair do |name, type|
data = ANI_fileIO.read(4)
data = data.unpack('L')[0] if type == :int
ParsedMeta[name] = data
warn " #{name}: #{data}"
end
warn "\n"
ParsedMeta[:anih_numFrames].times do |i|
File.open('%s_%02i.ico' % [Filename, i], 'w') do |fileIO|
chunkID = ANI_fileIO.read(4)
chunkSize = ANI_fileIO.read(4).unpack('L')[0]
abort 'Error!' unless chunkID == 'icon'
warn "Extracting frame #{i+1}/#{ParsedMeta[:anih_numFrames]}..."
fileIO.write ANI_fileIO.read(chunkSize)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment