Class: FormatParser::TIFFParser

Inherits:
Object
  • Object
show all
Includes:
IOUtils
Defined in:
lib/parsers/tiff_parser.rb

Constant Summary collapse

MAGIC_LE =
[0x49, 0x49, 0x2A, 0x0].pack('C4')
MAGIC_BE =
[0x4D, 0x4D, 0x0, 0x2A].pack('C4')

Instance Method Summary collapse

Methods included from IOUtils

#safe_read, #safe_skip

Instance Method Details

#call(io) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/parsers/tiff_parser.rb', line 7

def call(io)
  io = FormatParser::IOConstraint.new(io)

  return unless [MAGIC_LE, MAGIC_BE].include?(safe_read(io, 4))
  io.seek(io.pos + 2) # Skip over the offset of the IFD, EXIFR will re-read it anyway
  return if cr2?(io)

  # The TIFF scanner in EXIFR is plenty good enough,
  # so why don't we use it? It does all the right skips
  # in all the right places.
  scanner = FormatParser::EXIFParser.new(io)
  scanner.scan_image_tiff
  return unless scanner.exif_data

  FormatParser::Image.new(
    format: :tif,
    width_px: scanner.exif_data.image_width,
    height_px: scanner.exif_data.image_length,
    # might be nil if EXIF metadata wasn't found
    orientation: scanner.orientation
  )
rescue EXIFR::MalformedTIFF
  nil
end

#cr2?(io) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/parsers/tiff_parser.rb', line 32

def cr2?(io)
  io.seek(8)
  safe_read(io, 2) == 'CR'
end