Class: Identify::Image::TIFF

Inherits:
Identify::Image show all
Defined in:
lib/identify.rb

Overview

PCX

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Identify::Image

#as_hash, formats, identify, inherited, parse

Class Method Details

.handle?(data) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/identify.rb', line 52

def self.handle? data
  data[0..3] == "MM\x00\x2a" || data[0..3] == "II\x2a\x00"
end

Instance Method Details

#parse(data) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/identify.rb', line 56

def parse data
  endian  = data[0..1] == "MM" ? 'n' : 'v'
  # IFD offset
  offset  = data.unpack("x4#{endian.upcase}").first
  # Don't have enough header data
  return {} if data.bytesize < offset + 14

  nrec    = data[offset, 2].unpack(endian).first
  offset += 2
  height  = nil
  width   = nil
  types   = [nil, 'C', nil, endian, endian.upcase]

  while nrec > 0 && data.bytesize > offset + 12
    ifd  = data[offset, 12]
    type = ifd.unpack("x2#{endian}").first
    case ifd.unpack(endian).first
      when 0x0100
        width  = ifd[8, 4].unpack(types[type]).first
      when 0x0101
        height = ifd[8, 4].unpack(types[type]).first
    end
    nrec   -= 1
    offset += 12

    break if width && height
  end
  as_hash 'tiff', width, height
end