Class: Tiff::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/tiff/tag.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, id, type, map = nil) ⇒ Tag

Creates a new tag.

  • name: A rubyish name for the tag. Displayed to users for exceptions.

  • id: The value for this tag as defined by ‘tiff.h`

  • type: The FFI type of the tag

  • map: A hash of ruby to FFI values (optional)

Examples:

width = Tag.new(:width, 256, :uint)
metric = Tag.new(:photometric, 262, :ushort, {
  min_is_white: 0,
  min_is_black: 1
})


33
34
35
36
37
38
# File 'lib/tiff/tag.rb', line 33

def initialize(name, id, type, map = nil)
  @name = name
  @id = id
  @type = type
  @map = map
end

Instance Attribute Details

#idObject

The value for this tag as defined by ‘tiff.h`



8
9
10
# File 'lib/tiff/tag.rb', line 8

def id
  @id
end

#mapObject

A hash of ruby to FFI values for serialization and deserialization.



16
17
18
# File 'lib/tiff/tag.rb', line 16

def map
  @map
end

#nameObject

The name of this tag



5
6
7
# File 'lib/tiff/tag.rb', line 5

def name
  @name
end

#typeObject

The FFI type for this tag. See ‘man TIFFSetField` for the types of each field, and `github.com/ffi/ffi/wiki/Types` for it’s corresponding value in ruby.



13
14
15
# File 'lib/tiff/tag.rb', line 13

def type
  @type
end

Instance Method Details

#deserialize(pointer) ⇒ Object

Returns the deserialized value from the provided FFI::MemoryPointer.

If a map was provided for the tag, it will return the ruby value if it exists. If the mapping isn’t found, it will return the raw value.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tiff/tag.rb', line 57

def deserialize(pointer)
  case type
    when :string
      string = pointer.read_pointer()
      return string.null? ? nil : string.read_string()

    else
      value = pointer.send :"read_#{type}"
      value = map.invert[value] if map && map.has_value?(value)
      value
  end
end

#serialize(value) ⇒ Object

Returns the value for serialization. If the tag does not have a map defined, this simply returns the value provided.

If a map was provided for the tag, it will return the value for the mapping. If the mapping isn’t found, it will raise an exception.



45
46
47
48
49
50
51
# File 'lib/tiff/tag.rb', line 45

def serialize(value)
  return value unless map

  map.fetch value do
    raise KeyError, "Tag #{name.inspect} does not support value #{value.inspect}. Defined values: #{map.keys}"
  end
end