lib/ase/reader.rb in ase-1.0.3 vs lib/ase/reader.rb in ase-2.0.0

- old
+ new

@@ -28,48 +28,57 @@ # Okay maybe we should validate this in the future. @file.seek 8, IO::SEEK_CUR block_count = @file.read_ulong - read_palette until @file.eof? + @palette = Palette.new(:default) + read_section until @file.eof? + add_palette @palette if @palettes.length == 0 + @palette = nil + @file.close end private - def read_palette + def read_section block_start = @file.read_ushort + case block_start + when 0xC001 then start_palette + when 1 then read_color + when 0xC002 then end_palette + end + end + + def start_palette title_block_size = @file.read_ulong title = @file.read_string - palette = Palette.new(title) - - # Read the colors - while true - color_start = @file.read_ushort - break if color_start == 0xC002 + @palette = Palette.new(title) + end - color_size = @file.read_ulong - color_name = @file.read_string - color_mode = @file.read(4) + def end_palette + # Group end block + @file.seek 4, IO::SEEK_CUR + add_palette @palette - r, g, b = @file.read(12).scan(/.{1,4}/).map do |c| - (c.reverse.unpack('F')[0] * 255).to_i - end + @palette = Palette.new(:default) + end - palette.add color_name, Color.new(r, g, b) + def read_color + color_size = @file.read_ulong + color_name = @file.read_string + color_mode = @file.read(4) - # Null byte - @file.seek 2, IO::SEEK_CUR - end + color = Color.factory(color_mode) + color.read!(@file) - # Group end block - @file.seek 4, IO::SEEK_CUR - - add_palette palette - return true + @palette.add color_name, color + + # Null byte + @file.seek 2, IO::SEEK_CUR end end end end \ No newline at end of file