Sha256: 70c21685d6807c5b0efa52e0bd740c72fc0412442953a94175836333d82fade5

Contents?: true

Size: 1.14 KB

Versions: 16

Compression:

Stored size: 1.14 KB

Contents

class FormatParser::TIFFParser
  include FormatParser::IOUtils
  include FormatParser::EXIFParser

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

  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.
    exif_data = exif_from_tiff_io(io)
    return unless exif_data

    w = exif_data.image_width
    h = exif_data.image_length

    FormatParser::Image.new(
      format: :tif,
      width_px: w,
      height_px: h,
      display_width_px: exif_data.rotated? ? h : w,
      display_height_px: exif_data.rotated? ? w : h,
      orientation: exif_data.orientation,
      intrinsics: {exif: exif_data},
    )
  rescue EXIFR::MalformedTIFF
    nil
  end

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

  FormatParser.register_parser self, natures: :image, formats: :tif
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
format_parser-0.15.1 lib/parsers/tiff_parser.rb
format_parser-0.15.0 lib/parsers/tiff_parser.rb
format_parser-0.14.1 lib/parsers/tiff_parser.rb
format_parser-0.14.0 lib/parsers/tiff_parser.rb
format_parser-0.13.6 lib/parsers/tiff_parser.rb
format_parser-0.13.5 lib/parsers/tiff_parser.rb
format_parser-0.13.4 lib/parsers/tiff_parser.rb
format_parser-0.13.3 lib/parsers/tiff_parser.rb
format_parser-0.13.2 lib/parsers/tiff_parser.rb
format_parser-0.13.1 lib/parsers/tiff_parser.rb
format_parser-0.13.0 lib/parsers/tiff_parser.rb
format_parser-0.12.4 lib/parsers/tiff_parser.rb
format_parser-0.12.2 lib/parsers/tiff_parser.rb
format_parser-0.12.1 lib/parsers/tiff_parser.rb
format_parser-0.12.0 lib/parsers/tiff_parser.rb
format_parser-0.11.0 lib/parsers/tiff_parser.rb