Class: Pura::Gif::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/pura/gif/decoder.rb

Constant Summary collapse

INTERLACE_PASSES =
[
  [0, 8], # pass 1: every 8th row, starting at 0
  [4, 8], # pass 2: every 8th row, starting at 4
  [2, 4], # pass 3: every 4th row, starting at 2
  [1, 2] # pass 4: every 2nd row, starting at 1
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, transparency_color: [255, 255, 255]) ⇒ Decoder

Returns a new instance of Decoder.



29
30
31
32
33
# File 'lib/pura/gif/decoder.rb', line 29

def initialize(data, transparency_color: [255, 255, 255])
  @data = data
  @pos = 0
  @transparency_color = transparency_color
end

Class Method Details

.decode(input, transparency_color: [255, 255, 255]) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pura/gif/decoder.rb', line 15

def self.decode(input, transparency_color: [255, 255, 255])
  data = if input.is_a?(String) && input.encoding == Encoding::BINARY && input.start_with?("GIF")
           input
         elsif input.is_a?(String) && File.exist?(input)
           File.binread(input)
         elsif input.is_a?(String)
           input.b
         else
           raise DecodeError, "invalid input"
         end

  new(data, transparency_color: transparency_color).decode
end

Instance Method Details

#decodeObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pura/gif/decoder.rb', line 35

def decode
  read_header
  read_logical_screen_descriptor
  read_global_color_table if @gct_flag

  @transparent_index = nil

  # Read blocks until we find the first image
  loop do
    introducer = read_byte
    case introducer
    when 0x21 # Extension
      read_extension
    when 0x2C # Image Descriptor
      return read_image
    when 0x3B # Trailer
      raise DecodeError, "no image data found"
    else
      raise DecodeError, "unknown block type: 0x#{introducer.to_s(16)}"
    end
  end
end