Class: Camera

Inherits:
Object
  • Object
show all
Defined in:
lib/focusinspector/Camera.rb

Overview

Focus Inspector - The focus inspection and lens calibration software.

Copyright © 2012 by Chris Schlaeger <[email protected]>

This program is Open Source software; you can redistribute it and/or modify it under the terms of MIT license as shipped with this software.

Instance Method Summary collapse

Constructor Details

#initialize(imageFile) ⇒ Camera



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/focusinspector/Camera.rb', line 13

def initialize(imageFile)
  @imageFile = imageFile
  @cameras = {
               'NIKON D300' =>
               begin
                 h = {}
                 xSkip = 0.0676
                 ySkip = 0.095
                 # Rows A to E.
                 (-2).upto(2) do |row|
                   # Rows A and E have only 9 points, the others have 11.
                   cols = row.abs == 2 ? 4 : 5
                   (-cols).upto(cols) do |col|
                   h["#{(?A.ord + row + 2).chr}#{col + cols + 1}"] =
                       [ 0.5 + col * xSkip, 0.5 + row * ySkip ]
                   end
                 end
                 h['C6 (Center)'] = h['C6']
                 h
               end,
               'NIKON D5100' =>
               begin
                 x = [ 0.225, 0.330, 0.5 ]
                 y = [ 0.295, 0.385, 0.5 ]
                 x[3] = x[2] + (x[2] - x[1])
                 x[4] = x[2] + (x[2] - x[0])
                 y[3] = y[2] + (y[2] - y[1])
                 y[4] = y[2] + (y[2] - y[0])
                 {
                   'Far Left'    => [ x[0], y[2] ],
                   'Upper-left'  => [ x[1], y[1] ],
                   'Mid-left'    => [ x[1], y[2] ],
                   'Lower-left'  => [ x[1], y[3] ],
                   'Top'         => [ x[2], y[0] ],
                   'Center'      => [ x[2], y[2] ],
                   'Bottom'      => [ x[2], y[4] ],
                   'Upper-right' => [ x[3], y[1] ],
                   'Mid-right'   => [ x[3], y[2] ],
                   'Lower-right' => [ x[3], y[3] ],
                   'Far Right'   => [ x[4], y[2] ]
                 }
               end,
               'NIKON D800' =>
               begin
                 h = {}
                 # Rows A to E.
                 (-2).upto(2) do |row|
                   # Rows A and E have only 9 points, the others have 11.
                   cols = row.abs == 2 ? 4 : 5
                   (-cols).upto(cols) do |col|
                     # The 3 center columns have a wider x and y spread.
                     if col.abs <= 1
                       xSkip = 0.053
                       xOff = 0.0
                       ySkip = 0.075
                     else
                       xSkip = 0.048
                       xOff = 0.011 * (col < 0 ? -1 : 1)
                       ySkip = 0.0659
                     end
                     h["#{(?A.ord + row + 2).chr}#{col + cols + 1}"] =
                       [ 0.5 + xOff + col * xSkip, 0.5 + row * ySkip ]
                   end
                 end
                 h['C6 (Center)'] = h['C6']
                 h
               end
             }
  # Add some aliases for similar cameras. Just a guess right now.
  @cameras['NIKON D300S'] = @cameras['NIKON D300']
  @cameras['NIKON D800E'] = @cameras['NIKON D800']

  unless (@exif = MiniExiftool.new(@imageFile))
    Log.error("Image file #{imageFile} has no EXIF information")
  end
  unless (modelName = @exif['Model'])
    Log.error("No camera model name found in image file #{imageFile}")
  end
  unless (@focusPoints = @cameras[modelName])
    Log.error("Camera #{modelName} is not supported.")
  end
  @width = @exif['ImageWidth'].to_i
  @height = @exif['ImageHeight'].to_i
  @cx = @width / 2
  @cy = @height / 2
end

Instance Method Details

#activeFocusPointsObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/focusinspector/Camera.rb', line 161

def activeFocusPoints
  return [] if @exif['ContrastDetectAF'] == 'On'

  if (fps = @exif['AFPointsUsed']) == "(none)" or fps.nil?
    Log.error("Image has no focus point information")
  end

  allFPs = @focusPoints.keys.join(',')

  coords = []
  fps.split(',').each do |fp|
    unless (xy = fpCoords(fp))
      Log.error("Unknown focus point #{fp}")
    end
    coords << xy
  end
  coords
end

#contrastDetectAF?Boolean



114
115
116
# File 'lib/focusinspector/Camera.rb', line 114

def contrastDetectAF?
  @exif['ContrastDetectAF'] == 'On'
end

#focusAreaSizeObject



100
101
102
103
104
105
106
107
108
# File 'lib/focusinspector/Camera.rb', line 100

def focusAreaSize
  if (afW = @exif['AFAreaWidth']) && (afH = @exif['AFAreaHeight'])
    [ afW, afH ]
  else
    # We currently use a 27th of the screen width and height. This might have
    # to be made camera specific.
    [ @width / 27, @height / 27 ]
  end
end

#focusFineTuneObject



122
123
124
# File 'lib/focusinspector/Camera.rb', line 122

def focusFineTune
  @exif['AFFineTuneAdj']
end

#focusPointObject



118
119
120
# File 'lib/focusinspector/Camera.rb', line 118

def focusPoint
  @exif['Primary_AF_Point']
end

#inactiveFocusPointsObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/focusinspector/Camera.rb', line 180

def inactiveFocusPoints
  return [] if @exif['ContrastDetectAF'] == 'On'

  if (fps = @exif['AFPointsUsed']) == "(none)" or fps.nil?
    Log.error("Image has no focus point information")
  end
  fps = fps.split(',')

  allFPs = @focusPoints.keys

  coords = []
  (allFPs - fps).each do |fp|
    coords << fpCoords(fp)
  end
  coords
end

#orientationObject



110
111
112
# File 'lib/focusinspector/Camera.rb', line 110

def orientation
  @exif['Orientation'] || "Horizontal (normal)"
end

#primaryAutoFocusPointObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/focusinspector/Camera.rb', line 137

def primaryAutoFocusPoint
  if @exif['ContrastDetectAF'] != 'On'
    if (primaryAFP = @exif['Primary_AF_Point']) == "(none)" or primaryAFP.nil?
      Log.error("Image has no primary focus point information")
    end

    unless (coords = fpCoords(primaryAFP))
      Log.error("Unknown focus point #{primaryAFP}")
    end
    return coords
  else
    unless (x = @exif['AFAreaXPosition'])
      Log.error('Contrast detect focus X position not found')
    end
    x = x.to_i
    unless (y = @exif['AFAreaYPosition'])
      Log.error('Contrast detect focus Y position not found')
    end
    y = y.to_i

    return [ x, y ]
  end
end

#printDetailsObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/focusinspector/Camera.rb', line 126

def printDetails
  puts "Focus Mode:              #{@exif['FocusMode']}"
  puts "Contrast Detection:      #{@exif['ContrastDetectAF']}"
  puts "Phase Detection:         #{@exif['PhaseDetectAF']}"
  puts "AF Fine Tune:            #{@exif['AFFineTune']}"
  puts "AF Fine Tune Adjustment: #{@exif['AFFineTuneAdj']}"
  puts "Focus Distance:          #{@exif['FocusDistance']}"
  puts "Depth Of Field:          #{@exif['DOF']}"
  puts "Hyperfocal Distance:     #{@exif['HyperfocalDistance']}"
end