Sha256: ef2e3309815f91a13d9eb6225e39ba3c766e49f202dbc19233aacc6fff6a4e77

Contents?: true

Size: 1.86 KB

Versions: 3

Compression:

Stored size: 1.86 KB

Contents

require 'exifr/tiff'
require 'delegate'

module FormatParser::EXIFParser
  ORIENTATIONS = [
    :top_left,
    :top_right,
    :bottom_right,
    :bottom_left,
    :left_top,
    :right_top,
    :right_bottom,
    :left_bottom
  ]
  ROTATED_ORIENTATIONS = ORIENTATIONS - [
    :bottom_left,
    :bottom_right,
    :top_left,
    :top_right
  ]
  module MethodsMethodFix
    # Fix a little bug in EXIFR which breaks delegators
    # https://github.com/remvee/exifr/pull/55
    def methods(*)
      super() # no args
    end
  end
  EXIFR::TIFF.prepend(MethodsMethodFix)

  # EXIFR kindly requests the presence of a few more methods than what our IOConstraint
  # is willing to provide, but they can be derived from the available ones
  class IOExt < SimpleDelegator
    def readbyte
      if byte = read(1)
        byte.unpack('C').first
      end
    end

    def seek(n, seek_mode = IO::SEEK_SET)
      io = __getobj__
      case seek_mode
      when IO::SEEK_SET
        io.seek(n)
      when IO::SEEK_CUR
        io.seek(io.pos + n)
      when IO::SEEK_END
        io.seek(io.size + n)
      else
        raise Errno::EINVAL
      end
    end

    alias_method :getbyte, :readbyte
  end

  class EXIFResult < SimpleDelegator
    def rotated?
      ROTATED_ORIENTATIONS.include?(orientation)
    end

    def to_json(*maybe_coder)
      hash_representation = __getobj__.to_hash
      sanitized = FormatParser::AttributesJSON._sanitize_json_value(hash_representation)
      sanitized.to_json(*maybe_coder)
    end

    def orientation
      value = __getobj__.orientation.to_i
      ORIENTATIONS.fetch(value - 1)
    end
  end

  # Squash exifr's invalid date warning since we do not use that data.
  EXIFR.logger = Logger.new(nil)

  def exif_from_tiff_io(constrained_io)
    raw_exif_data = EXIFR::TIFF.new(IOExt.new(constrained_io))
    raw_exif_data ? EXIFResult.new(raw_exif_data) : nil
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
format_parser-0.13.6 lib/parsers/exif_parser.rb
format_parser-0.13.5 lib/parsers/exif_parser.rb
format_parser-0.13.4 lib/parsers/exif_parser.rb