Class: Halation::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/halation/engine.rb

Overview

Performs modifications to the image Exif data.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Engine

Returns a new instance of Engine.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :config_path (String) — default: nil

    Override the config file that should be used.

  • :working_dir (String) — default: "."

    Override the working directory (contains images and roll.yml).

  • :image_extensions (Array<String>)

    Override the list of image extensions to search for.

  • :silent (Boolean)

    Suppress output to stdout.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/halation/engine.rb', line 29

def initialize(opts = {})
  config_path = opts[:config_path]
  @silent = !!opts[:silent]
  @working_dir = File.expand_path(opts[:working_dir] || ".")
  @image_extensions = opts[:image_extensions] || ["tif", "tiff", "jpg", "jpeg"]

  @config = Config.new
  @config.load_file(config_path)

  @roll = Roll.new
  @roll.load_file("#{@working_dir}/roll.yml")
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/halation/engine.rb', line 13

def config
  @config
end

#image_extensionsObject

Array of image extensions to search for.



9
10
11
# File 'lib/halation/engine.rb', line 9

def image_extensions
  @image_extensions
end

#rollObject (readonly)

Returns the value of attribute roll.



14
15
16
# File 'lib/halation/engine.rb', line 14

def roll
  @roll
end

#silentObject

Suppress output to stdout if true.



11
12
13
# File 'lib/halation/engine.rb', line 11

def silent
  @silent
end

Class Method Details

.run(opts = {}) ⇒ Object

See Also:



17
18
19
# File 'lib/halation/engine.rb', line 17

def self.run(opts = {})
  new(opts).run
end

Instance Method Details

#image_filesArray<String>

Returns detected image files to process, in ascending alphabetical order.

Returns:

  • (Array<String>)

    detected image files to process, in ascending alphabetical order.



44
45
46
47
# File 'lib/halation/engine.rb', line 44

def image_files
  Dir[*@image_extensions.map { |ext| "#{@working_dir}/*.#{ext}" }]
    .sort[0...@roll.frames.count]
end

#runObject

Process the batch of images.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/halation/engine.rb', line 50

def run
  _image_files = image_files

  specified_camera = @roll.camera
  camera = @config.cameras.find { |camera| camera.tag == specified_camera }
  raise "Camera #{specified_camera} not found in config.yml" unless camera

  @roll.frames.each_with_index do |frame, i|
    break if i >= _image_files.count

    specified_lens = frame.lens || @roll.lens
    lens = camera.lenses.find { |lens| lens.tag == specified_lens }
    raise "Lens #{specified_lens} not found for frame #{frame.number}" unless lens

    relative_path = _image_files[i].gsub(%r(^#{Dir.pwd}/), "")
    puts "Processing frame #{frame.number}\n   #{relative_path}" unless @silent

    ExifToolImage.new(_image_files[i]).tap do |exif|
      exif["Artist"] = @roll.artist || @config.artist
      exif["Copyright"] = template_copyright(@config, @roll, frame)
      exif["DateTimeOriginal"] = frame.date_captured || @roll.date_captured
      exif["CreateDate"] = frame.date_scanned || @roll.date_scanned
      exif["Make"] = camera.make
      exif["Model"] = camera.model
      exif["LensModel"] = lens.model
      exif["ISO"] = @roll.iso
      exif["ExposureTime"] = frame.shutter || @roll.shutter
      exif["FNumber"] = frame.aperture || @roll.aperture
      exif["FocalLength"] = frame.focal_length || lens.focal_length || @roll.focal_length
      exif["Flash"] = frame.flash ? 1 : 0
      exif["Orientation"] = frame.orientation || 1

      exif.save
    end
  end
end