Class: Klear::FileGenerator

Inherits:
Object
  • Object
show all
Includes:
Java
Defined in:
lib/klear/file_generator.rb

Constant Summary collapse

Defaults =
{
  overwrite: false, 
  fps: 25,
  gamma: 1.0,
  pixel_scale: [10, 10],
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FileGenerator

Returns a new instance of FileGenerator.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/klear/file_generator.rb', line 10

def initialize options = {}
  @options = Defaults.merge(options)
  @fps = @options[:fps]
  @gamma = @options[:gamma]

  @raw_frame_values = []
  @png_path = nil
  @kle_path = nil
  @geometry = nil
  @kle_file = nil
end

Instance Method Details

#generate(thePngPath, theKleFile) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/klear/file_generator.rb', line 65

def generate thePngPath, theKleFile
  @png_path = thePngPath
  @kle_path = theKleFile
  if (File.exists? @kle_path) 
    if overwrite?
      FileUtils.rm(@kle_path, force: true)
    else
      raise "'#{@kle_path}' :: already exists! (use --overwrite to force it)"
    end
  end
  write(@kle_path)
  report
end

#loadObject



30
31
32
# File 'lib/klear/file_generator.rb', line 30

def load
  Zip::ZipFile.open(@kle_path) {|kle| @kle_file = kle}
end

#overwrite?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/klear/file_generator.rb', line 26

def overwrite?
  @options[:overwrite]
end

#pixel_scaleObject



22
23
24
# File 'lib/klear/file_generator.rb', line 22

def pixel_scale
  @options[:pixel_scale]
end

#regenerate(theKleFile) ⇒ Object



95
96
97
98
99
# File 'lib/klear/file_generator.rb', line 95

def regenerate theKleFile
  @kle_path = theKleFile
  load
  recache
end

#reportObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/klear/file_generator.rb', line 79

def report
  report = "\nInput  Directory : '\#{@png_path}'\nOutput KleFile : '\#{@kle_path}'\n\nDetails:\n  * number of [png files|frames]: \#{@kle_file.dir.entries(\"/frames\").size}\n  * fps: \#{@fps}\n  * gamma : \#{@gamma}\n  * geometry : \#{@geometry}\n\n  REPORT\n  puts report if !$silent\nend\n"

#write(path) ⇒ Object



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
# File 'lib/klear/file_generator.rb', line 34

def write(path)
  # TODO:  maybe use Zip::ZipOutputStream to generate zip file in-memory?
  Zip::ZipFile.open(path, Zip::ZipFile::CREATE) do |klear|
    @kle_file = klear
    add_pngs(@kle_file)
    recache
    
    # Meta
    klear.mkdir('META-INF')
    klear.file.open("META-INF/MANIFEST.MF", "w") do |fd|
      fd.write "Manifest-Version: 1.0\n\nKle-Version: 1.1\nCreated-By: \#{__FILE__} (\#{Klear::VERSION})\nGenerated-At: \#{Time.now}\n\n      MANIFEST\n    end\n\n    klear.file.open(\"META-INF/kle.yml\", \"w\") do |fd|\n      fd.write({\n        \"geometry\"    => @geometry,\n        \"pixel_scale\" => pixel_scale,\n        \"gamma\"       => @gamma,\n        \"fps\"         => @fps\n      }.to_yaml)\n    end\n  end\nend\n"