Class: Icns::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/icns/reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Reader

Initialize with a path to a valid ICNS file



8
9
10
11
12
13
14
15
# File 'lib/icns/reader.rb', line 8

def initialize(path)
  throw FileNotFound unless File.exists?(path)

  @path = path
  @parts = {}

  
end

Instance Method Details

#data_for_type(type) ⇒ Object

Raw data for a type



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/icns/reader.rb', line 23

def data_for_type(type)
  return nil unless part = @parts[type.to_s]

  File.open(@path) do |file|
    # Seek to the image data position
    file.pos = part[:offset]

    # Read the length of the image data
    file.read(part[:length])
  end
end

#image_for_size(size) ⇒ Object

PNG or JPEG-2000 data for a size



36
37
38
39
40
41
42
43
44
45
# File 'lib/icns/reader.rb', line 36

def image_for_size(size)
  return nil unless types = SIZE_TO_TYPE[size.to_i]

  data = nil
  types.each do |type|
    break if data = data_for_type(type)
  end

  data
end

#sizesObject

Available image sizes



48
49
50
51
52
# File 'lib/icns/reader.rb', line 48

def sizes
  types = SIZE_TO_TYPE.values.flatten
  @parts.keys.select { |k| types.include?(k) }.
    map { |k| TYPE_TO_SIZE[k] }.sort.uniq
end

#typesObject

Available types



18
19
20
# File 'lib/icns/reader.rb', line 18

def types
  @parts.keys
end