Class: Icns::Reader

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

Overview

Read an ICNS file’s metadata and extract images.

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Reader

Initialize with a path to a valid ICNS file



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

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

  @path = path
  @parts = {}

  
end

Instance Method Details

#data(type:) ⇒ Object

Raw data for a type



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

def data(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(size:) ⇒ Object

PNG or JPEG-2000 data for a size



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

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

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

  data
end

#sizesObject

Available image sizes



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

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



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

def types
  @parts.keys
end