Sha256: af01289eaca62ae94b8372a58376b698f627fe5f12c9ea2a58601a199d2618f5

Contents?: true

Size: 1.85 KB

Versions: 4

Compression:

Stored size: 1.85 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
      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
    @orientation = ORIENTATIONS[value - 1] if valid_orientation?(value)
  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'
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
format_parser-0.3.3 lib/parsers/exif_parser.rb
format_parser-0.3.2 lib/parsers/exif_parser.rb
format_parser-0.3.1 lib/parsers/exif_parser.rb
format_parser-0.3.0 lib/parsers/exif_parser.rb