-
Notifications
You must be signed in to change notification settings - Fork 0
/
bif.rb
56 lines (49 loc) · 1.42 KB
/
bif.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# BIF Writer for Ruby
# by Derik Olsson <[email protected]>
# adapted from: bcl's HMS "makeabif.py" script: https://github.com/bcl/HMS/blob/master/scripts/makebif.py
class BIF
class Writer
VERSION = 0
def initialize(options = {})
if !options[:dir].nil?
if File.directory?(options[:dir])
@images = Dir[File.join(options[:dir], '*.jpg')].sort!
@interval = options.fetch(:interval, '15').to_i * 1000
else
puts "INVALID DIRECTORY: #{options[:dir]}"
end
else
puts 'ABORT: Requires directory'
exit
end
end
def write_bif_file_header(file)
file << [0x89, 0x42, 0x49, 0x46, 0x0d, 0x0a, 0x1a, 0x0a].pack('C*')
file << [VERSION, @images.length, @interval].pack('I3')
file << [].fill(0x00, nil, 44).pack('C*')
end
def write_bif_index(file)
bif_table_size = 8 + (8 * @images.length)
frame_offset = 64 + bif_table_size
i = 0
@images.each do |img|
file << [i, frame_offset].pack('I2')
i += 1
frame_offset += File.new(img).size
end
file << [0xffffffff, frame_offset].pack('I2')
end
def write_frames(file)
@images.each do |img|
file << IO.binread(img)
end
end
def write(filename)
File.open(filename, 'wb') do |file|
write_bif_file_header(file)
write_bif_index(file)
write_frames(file)
end
end
end
end