Sha256: 1f5d0a4f0763da48dcb140841a2a709672ed850da4b149e7a92e7131ada966d7

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 KB

Contents

require 'exifr/jpeg'
require 'exifr/tiff'
require 'delegate'

class FormatParser::EXIFParser
  include FormatParser::IOUtils

  # EXIFR kindly requests the presence of getbyte and readbyte
  # IO methods, which our constrained IO subset does not provide natively
  class IOExt < SimpleDelegator
    def readbyte
      if byte = read(1)
        byte.unpack('C').first
      else
        nil
      end
    end

    alias_method :getbyte, :readbyte
  end

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

  attr_accessor :exif_data, :orientation, :width, :height

  ORIENTATIONS = [
    :top_left,
    :top_right,
    :bottom_right,
    :bottom_left,
    :left_top,
    :right_top,
    :right_bottom,
    :left_bottom
  ]

  def initialize(filetype, file_io)
    @filetype = filetype
    @file_io = IOExt.new(file_io)
    @exif_data = nil
    @orientation = nil
    @height = nil
    @width = nil
  end

  def scan_image_exif
    # Without the magic bytes EXIFR throws an error
    @file_io.seek(0)
    raw_exif_data = EXIFR::JPEG.new(@file_io) if @filetype == :jpeg
    raw_exif_data = EXIFR::TIFF.new(@file_io) if @filetype == :tiff
    # For things that we don't yet have a parser for
    # we make the raw exif result available
    @exif_data = raw_exif_data
    @orientation = orientation_parser(raw_exif_data)
    @width = @exif_data.width
    @height = @exif_data.height
  end

  def orientation_parser(raw_exif_data)
    value = raw_exif_data.orientation.to_i
    if valid_orientation?(value)
      @orientation = ORIENTATIONS[value - 1]
    end
  end

  def valid_orientation?(value)
    (1..ORIENTATIONS.length).include?(value)
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
format_parser-0.1.6 lib/parsers/exif_parser.rb
format_parser-0.1.5 lib/parsers/exif_parser.rb
format_parser-0.1.4 lib/parsers/exif_parser.rb
format_parser-0.1.3 lib/parsers/exif_parser.rb
format_parser-0.1.2 lib/parsers/exif_parser.rb