Class: Pura::Ico::Decoder

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

Constant Summary collapse

ICO_TYPE =
1
CUR_TYPE =
2
PNG_SIGNATURE =
[137, 80, 78, 71, 13, 10, 26, 10].pack("C8").freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Decoder

Returns a new instance of Decoder.



22
23
24
25
# File 'lib/pura/ico/decoder.rb', line 22

def initialize(data)
  @data = data
  @pos = 0
end

Class Method Details

.decode(input) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/pura/ico/decoder.rb', line 13

def self.decode(input)
  data = if input.is_a?(String) && !input.include?("\x00") && input.bytesize < 4096 && File.exist?(input)
           File.binread(input)
         else
           input.b
         end
  new(data).decode
end

Instance Method Details

#decodeObject

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pura/ico/decoder.rb', line 27

def decode
  # Parse ICO header
  read_uint16
  type = read_uint16
  count = read_uint16

  raise DecodeError, "Not an ICO/CUR file (type=#{type})" unless [ICO_TYPE, CUR_TYPE].include?(type)

  raise DecodeError, "No images in ICO file" if count.zero?

  # Parse directory entries
  entries = Array.new(count) { read_directory_entry(type) }

  # Find the largest image (by pixel area)
  best = entries.max_by { |e| e[:width] * e[:height] }

  decode_entry(best)
end