Sha256: a5c4502d1ce043b7a95401f96b69075416aa4f9c5b9fe2ad37bb679dd7b5c549

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 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
    # Return if it's a CR2, which we don't parse yet
    return if cr2_check(@file_io)
    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

  def cr2_check(file_io)
    @file_io.seek(8)
    cr2_check_bytes = @file_io.read(2)
    cr2_check_bytes == "CR" ? true : false
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
format_parser-0.2.0 lib/parsers/exif_parser.rb
format_parser-0.1.7 lib/parsers/exif_parser.rb