File: check_camera_support

package info (click to toggle)
darktable 5.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 62,864 kB
  • sloc: ansic: 361,898; cpp: 102,446; xml: 19,813; lisp: 14,539; sh: 3,771; javascript: 3,264; perl: 1,925; python: 1,485; ruby: 975; makefile: 543; asm: 46; sql: 38; awk: 21
file content (359 lines) | stat: -rwxr-xr-x 11,460 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env ruby

require 'nokogiri'
require 'json'

class CameraSupportState
  SOURCE_FILES={
    :cameras => "src/external/rawspeed/data/cameras.xml",
    :wbpresets => "data/wb_presets.json",
    :colormatrices => "src/common/colormatrices.c",
    :noiseprofiles => "data/noiseprofiles.json",
  }
  PANASONIC_NEEDED_FORMATS=["4:3", "3:2", "16:9", "1:1"]

  def initialize(opts={})
    @samplesdir = opts[:samplesdir]
    @basedir=Dir.pwd
    opts[:ref] ||= 'HEAD'
    @submodules=get_submodules(opts[:ref])
    @sources = Hash[
      SOURCE_FILES.map do |name, file|
        [name, file_content(file, opts[:ref])]
      end
    ]
    process_files
  end

  def file_content(filename, rev)
    submodule,submodulerev=get_submodule(filename)
    goodname = filename.dup
    if submodule
      Dir.chdir(submodule)
      goodname.gsub!("#{submodule}/",'')
      rev=submodulerev
    end
    ret=IO.popen("git show #{rev}:#{goodname}")
    if submodule
      Dir.chdir(@basedir)
    end
    ret
  end

  def get_submodules(rev)
    ret={}
    IO.popen("git ls-tree --full-tree -z -r #{rev}") do |fh|
      fh.each_line("\0") do |line|
        # 100644 blob bc16048078e48ccfa6a1aaa539fa295e7d9f2601    tools/wb_presets_common.rb
        mo=/\A(?<mode>\d+) (?<type>\S+) (?<hash>\S+)\t(?<path>\S+)\0\z/.match(line)
        if mo and (mo[:type] == 'commit')
          ret[mo[:path]]=mo[:hash]
        end
      end
    end
    ret
  end

  def get_submodule(filename)
    submodule=@submodules.select {|dir,hash| filename.start_with?(dir) }.first
  end

  def get_maker_model(file)
    def get_exif_key(key, file)
      f = IO.popen("exiv2 -g \"#{key}\" -Pt \"#{file}\" 2>/dev/null","r")
      c = f.read
      f.close
      return c
    end

    maker = get_exif_key("Exif.Image.Make", file)
    maker = maker[0..6] == "SAMSUNG" ? "SAMSUNG" : maker.strip
    model = get_exif_key("Exif.Image.Model", file)
    model = model[0..5] == "NX2000" ? "NX2000" : model.strip

    if (maker == "" || model == "") # Try with rawspeed instead
      f = IO.popen("/opt/darktable/bin/darktable-rs-identify \"#{file}\"","r")
      f.each do |line|
        parts = line.split(":")
        case parts[0].strip
        when "make"
          maker = parts[1..-1].join(":").strip
        when "model"
          model = parts[1..-1].join(":").strip
        end
      end
      f.close
    end
    return [maker,model]
  end

  def process_files
    @rawspeed_cameras = {}
    @rawspeed_aliases = {}
    @rawspeed_panasonic_formats = {}
    @rawspeed_dngs = {}
    @rawspeed_modes = {}
    @exif_name_map = {}
    @exif_alias_map = {}
    xml_doc  = Nokogiri::XML(@sources[:cameras])
    xml_doc.css("Camera").each do |c|
      maker = exif_maker = c.attribute("make").value
      model = c.attribute("model").value
      exif_id = "#{maker} #{model}"
      if c.css("ID")[0]
        maker = c.css("ID")[0].attribute("make").value
        model = c.css("ID")[0].attribute("model").value
      end
      id = "#{maker} #{model}"
      supported = !c.attribute("supported") || c.attribute("supported").value == "yes"
      if supported
        @rawspeed_cameras[id] = 0
        @rawspeed_aliases[id] = [id]
        @exif_name_map[exif_id] = id
        @exif_alias_map[exif_id] = id
        mode = ""
        mode = c.attribute("mode").value if c.attribute("mode")
        mode = "DNG" if mode == "dng"
        mode = "CHDK" if mode == "chdk"
        if mode != ""
          @rawspeed_modes[id] ||= []
          @rawspeed_modes[id] <<= mode
        end
        @rawspeed_dngs[id] = true if mode == "DNG"
        if PANASONIC_NEEDED_FORMATS.include?(mode)
          @rawspeed_panasonic_formats[id] ||= []
          @rawspeed_panasonic_formats[id] << mode
        end
        c.css("Alias").each do |a|
          exif_model = model = a.content
          exif_id = "#{exif_maker} #{exif_model}"
          model = a.attribute("id").value if a.attribute("id")
          aliasid = "#{maker} #{model}"
          @rawspeed_aliases[id] << aliasid if aliasid != id
          @exif_name_map[exif_id] = id
          @exif_alias_map[exif_id] = aliasid
          if mode != ""
            @rawspeed_modes[aliasid] ||= []
            @rawspeed_modes[aliasid] <<= mode
          end
        end
      end
    end
    @rawspeed_cameras = @rawspeed_cameras.keys

    @presets_cameras = {}
    JSON.parse(@sources[:wbpresets].read)['wb_presets'].each do |mak|
      maker = mak['maker']
      mak['models'].each do |mod|
        model = mod['model']
        @presets_cameras["#{maker} #{model}"] = 0
      end
    end

    @colormatrices_cameras = {}
    @sources[:colormatrices].each do |line|
      if line[0..2] == "  {"
        @colormatrices_cameras[line.split('"')[1]] = 0
      end
    end

    @noiseprofiles_cameras = {}
    JSON.parse(@sources[:noiseprofiles].read)['noiseprofiles'].each do |mak|
      maker = mak['maker']
      mak['models'].each do |mod|
        model = mod['model']
        @noiseprofiles_cameras["#{maker} #{model}"] = 0
      end
    end

    @samples_cameras = {}
    @samples_alias_cameras = {}
    if @samplesdir
      Dir["#{@samplesdir}/**/**/*"].each do |file|
        if (File.file?(file))
          maker, model = get_maker_model(file)
          if (maker != "" && model != "")
            id = name = alias_name = "#{maker} #{model}"
            name = @exif_name_map[id] if @exif_name_map[id]
            alias_name = @exif_alias_map[id] if @exif_alias_map[id]
            @samples_cameras[name] = 0
            @samples_alias_cameras[alias_name] = 0
          end
        end
      end
    end
  end

  def compare_lists(name, cameras, db, verbose_miss, verbose_nomatch)
    miss_cams = []
    cameras.each do |c|
      if !db[c]
        miss_cams << c
      else
        db[c] += 1
      end
    end

    miss_db = []
    db.each do |c, num|
      if num == 0
        miss_db << c
      end
    end

    puts "For #{name} found #{miss_cams.size} cameras missing and #{miss_db.size} entries for no cam"
    miss_cams.each {|c| puts "  MISS: #{c}"} if verbose_miss
    miss_db.each {|c| puts "  NOMATCH: #{c}"} if verbose_nomatch
  end

  def default_listing(verbose_mode, quiet_mode)
    puts "Found #{@rawspeed_cameras.size} cameras #{@presets_cameras.size} wb_coeffs #{@colormatrices_cameras.size} colormatrices #{@noiseprofiles_cameras.size} noise profiles #{@samples_cameras.size} samples"
    rawspeed_coeffs_cameras = @rawspeed_cameras.select{|id| !@rawspeed_dngs[id]}
    compare_lists("wb_presets", @rawspeed_cameras, @presets_cameras, verbose_mode, !quiet_mode)
    compare_lists("colormatrices", @rawspeed_cameras, @colormatrices_cameras, verbose_mode, !quiet_mode)
    compare_lists("noiseprofiles", @rawspeed_cameras, @noiseprofiles_cameras, verbose_mode, !quiet_mode)
    compare_lists("samples", @rawspeed_cameras, @samples_cameras, verbose_mode, !quiet_mode) if @samplesdir
    @rawspeed_panasonic_formats.each do |camera, formats|
      if formats.size != 4
        missing_formats = PANASONIC_NEEDED_FORMATS.select{|f| !formats.include?(f)}
        puts "Missing formats for '#{camera}' #{missing_formats.join(' ')}"
      end
    end
  end

  def matrix_listing
    puts "<!-- valid as of #{Time.now.strftime("%Y/%m/%d")}, #{IO.popen("git describe").readlines[0]} -->"
    puts "<table class=\"smalltext altrows u-full-width\">"

    counts = [0, 0, 0, 0]
    clist = ""
    i = 1
    camera_list.each do |camera, al|
      counts[0] += 1
      i += 1
      clist << "  <tr>"
      clist << "<td>#{al}</td>"
      [@presets_cameras[camera],
       @noiseprofiles_cameras[camera], @colormatrices_cameras[camera]].each_with_index do |value, num|
        counts[num+1] += 1 if value
        value = value ? "Yes" : "<strong>NO</strong>"
        clist << "<td>#{value}</td>"
      end
      clist << "</tr>\n"
    end

    puts "  <thead><tr>"
    ["Camera", "WB Presets", "Noise Profile", "Custom Matrix"].each_with_index do |name, i|
      $stdout.write "    <th><strong>#{name}</strong><br/>"
      $stdout.write "#{counts[i]} (#{(counts[i].to_f/counts[0].to_f*100).to_i}%)"
      $stdout.write "</th>\n"
    end
    puts "  </tr></thead>"
    $stdout.write clist
    puts "</table>"
  end

  def camera_list
    list = []
    @rawspeed_cameras.sort.each do |camera|
      @rawspeed_aliases[camera].sort.each do |al|
        list << [camera, al]
      end
    end
    list
  end

  attr_reader :rawspeed_modes
  attr_reader :presets_cameras
  attr_reader :noiseprofiles_cameras
  attr_reader :colormatrices_cameras
  attr_reader :exif_alias_map

  def diff_listing(from)
    new_basics   = []
    new_presets  = []
    new_profiles = []
    new_matrices = []

    from_cameras = Hash[from.camera_list.map{|camera, al| [al, true]}]

    # Create a reverse map between alias and possible EXIF names
    alias_exif_map = {}
    @exif_alias_map.each do |exif, al|
      alias_exif_map[al] ||= []
      alias_exif_map[al] <<= exif
    end

    camera_list.each do |camera, al|
      fromals = alias_exif_map[al].map{|exif| from.exif_alias_map[exif]}
      fromals.delete nil
      if (!from_cameras[al] && # Camera was not listed before
         fromals.size == 0) || # And it wasn't just an ID rename
         from.rawspeed_modes[al] != @rawspeed_modes[al] # Or it has new modes
        modes = (@rawspeed_modes[al]||[]).dup
        (from.rawspeed_modes[al]||[]).each {|m| modes.delete m}
        new_basics << al+(modes.size>0 ? " ("+modes.join(", ")+")" : "")
      end

      def test_camera(to_cameras, cam, from_cameras, fromals)
        return false if !to_cameras[cam] # It doesn't exist in the new version
        return false if from_cameras[cam] # It already existed in the old version
        fromals.each{|fromal| return false if from_cameras[fromal]} # It wasn't just an ID rename
        return true
      end

      new_presets << al  if test_camera(@presets_cameras, camera, from.presets_cameras, fromals)
      new_profiles << al if test_camera(@noiseprofiles_cameras, camera, from.noiseprofiles_cameras, fromals)
      new_matrices << al if test_camera(@colormatrices_cameras, camera, from.colormatrices_cameras, fromals)
    end

    def print_list(name, list)
      return if list.size == 0
      puts name
      puts
      list.each do |item|
        puts "- "+item
      end
      puts
    end

    print_list "### Base Support", new_basics.uniq
    print_list "### White Balance Presets", new_presets.uniq
    print_list "### Noise Profiles", new_profiles.uniq
    print_list "### Custom Color matrices", new_matrices.uniq
  end
end #class CameraSupportState

if ARGV[0] == "--compare" && ARGV.size == 3
  from = CameraSupportState.new(:ref=>ARGV[1])
  to = CameraSupportState.new(:ref=>ARGV[2])
  to.diff_listing(from)
else
  matrix_mode = nil
  verbose_mode = false
  quiet_mode = false
  samplesdir = nil

  ARGV.each do |arg|
    if arg == "--matrix"
      matrix_mode = true
    elsif arg == "--verbose"
      verbose_mode = true
    elsif arg == "--quiet"
      quiet_mode = true
    elsif samplesdir == nil && File.exist?(arg)
      samplesdir = arg
    else
      $stderr.puts "Usage: check_camera_support [--verbose] [--matrix] <samples dir>"
      $stderr.puts "       check_camera_support --compare <from git> <to git>"
      exit 2
    end
  end

  css = CameraSupportState.new(:samplesdir => samplesdir)
  if matrix_mode
    css.matrix_listing
  else
    css.default_listing(verbose_mode, quiet_mode)
  end
end