Module: ImageSvd::Options

Extended by:
Util
Defined in:
lib/image_svd/cli.rb

Overview

This module holds custom behavior for dealing with the gem trollop

Constant Summary

Constants included from Util

Util::IMAGE_CREDIT, Util::VALID_INPUT_EXT_REGEX

Class Method Summary collapse

Methods included from Util

extension_swap

Class Method Details

.collect_input(opts) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/image_svd/cli.rb', line 176

def self.collect_input(opts)
  %x(mkdir #{opts[:input_file].path + 'out'}) if opts[:directory]
  collection = []
  expand_input_files(opts).each do |file|
    new_options = { input_file: file }
    if opts[:directory]
      new_options.merge!(output_name: output_dir_path_for_input_file(file))
    end
    collection << process(opts).merge(new_options)
  end
  collection
end

.expand_input_files(opts) ⇒ Object

reformats directory inputs into an array of files, or repackages file inputs to be contained inside an array



158
159
160
161
162
163
164
165
166
167
# File 'lib/image_svd/cli.rb', line 158

def self.expand_input_files(opts)
  if opts[:directory]
    path = opts[:input_file].path
    names = Dir.new(path).to_a
    images = names.select { |name| name =~ Util::VALID_INPUT_EXT_REGEX }
    images.map { |name| File.new(path + name) }
  else
    [opts[:input_file]]
  end
end

.format_num_sing_vals(str) ⇒ Object

reformats the string cmd line option to an array



144
145
146
147
148
149
# File 'lib/image_svd/cli.rb', line 144

def self.format_num_sing_vals(str)
  i, valid_i_regex = [str, /^\d+\.\.\d+$|^\d+$/]
  fail 'invalid --num-singular-values option' unless i.match valid_i_regex
  vs = i.split('..').map(&:to_i)
  vs.length == 1 ? vs : ((vs.first)..(vs.last)).to_a
end

.getObject

rubocop:disable MethodLength



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
# File 'lib/image_svd/cli.rb', line 48

def self.get
  proc do
    version "Image Svd #{ImageSvd::VERSION} (c) 2014 Ilya Kavalerov"
    banner "\n              _____________   ____          ____   ______\n              \\\\    ________\\\\  \\\\   \\\\        /   /  /  __  \\\\\n               \\\\   \\\\           \\\\   \\\\      /   /  /  /  \\\\  \\\\\n                \\\\   \\\\________   \\\\   \\\\    /   /  /  /    \\\\  \\\\\n                 \\\\_________   \\\\  \\\\   \\\\  /   /  /  /     /  /\n                           \\\\   \\\\  \\\\   \\\\/   /  /  /     /  /\n                   _________\\\\   \\\\  \\\\      /  /  /_____/  /\n                   \\\\_____________\\\\  \\\\____/  /___________/\n\n\n      Image Svd is a utilty for compressing images, or creating\n      interesting visual effects to distort images when compression is\n      set very high. Image Svd performs Singular Value Decomposition\n      on any image, grayscale or color.\n\n      Usage:\n             image_svd [options]\n      where [options] are:\n \n    EOS\n    opt :input_file,\n        'An input file (Preferably a jpg). If you also specify'\\\n          ' --directory or -d, you may provide the path to a directory'\\\n          ' (which must end with a \"/\") instead of a file.',\n        type: :io,\n        required: true\n    opt :grayscale,\n        'Do not preserve the colors in the input image. Specify'\\\n          ' --no-grayscale when you want an output image in color.'\\\n          ' Expect processing time to increase 3-fold for color images.',\n        default: true,\n        short: '-g'\n    opt :num_singular_values,\n        'The number of singular values to keep for an image. Lower'\\\n          ' numbers mean lossier compression, smaller files and more'\\\n          ' distorted images. You may also provide a range ruby style'\\\n          ' (ex: 1..9) in which case many images will be output.',\n        default: '50',\n        short: '-n'\n    opt :output_name,\n        'A path/name for an output file (Extension will be ignored).'\\\n          ' If no path/name is provided, a file will be written in'\\\n          ' the current directory',\n        default: 'svd_image_output',\n        short: '-o'\n    opt :directory,\n        'The input provided is a directory instead of a file. In this'\\\n          ' case every valid image inside the directory provided with'\\\n          ' the option -i will be compressed, and placed into a folder'\\\n          ' named \"out\" inside the directory specified.',\n        default: false,\n        short: '-d'\n    opt :convert,\n        'Convert the input file now.',\n        default: true,\n        short: '-c'\n    opt :archive,\n        'Save the Image Svd archive without converting the input image.',\n        default: false,\n        short: '-a'\n    opt :read,\n        'Read an Image Svd archive (*.svdim) and output the image'\\\n          ' it contains.',\n        default: false,\n        short: '-r'\n    opt :thread_count,\n        'Advanced feature. The amount of separate threads to use when'\\\n          ' processing images. Relevant when processing many images in'\\\n          ' a directory. Should not exceed (n - 1) where n is the'\\\n          ' logical core count on the end user\\'s computer. Otherwise,'\\\n          ' a higher number means less total time spent processing.',\n        default: 3,\n        short: '-t'\n  end\nend\n"

.num_sing_val_out_from_archive(requests, available) ⇒ Object

this method chooses which number of singular values are valid to output to an image file from an archive file provided. @returns Array



132
133
134
135
# File 'lib/image_svd/cli.rb', line 132

def self.num_sing_val_out_from_archive(requests, available)
  valid_svals = requests.reject { |v| v > available }
  valid_svals.empty? ? [available] : valid_svals
end

.output_dir_path_for_input_file(dir) ⇒ Object

ignore provided output_name in the case that a directory is input



170
171
172
173
174
# File 'lib/image_svd/cli.rb', line 170

def self.output_dir_path_for_input_file(dir)
  path_components = dir.path.split('/')
  filename = path_components.pop
  (path_components << 'out' << filename).join('/')
end

.process(opts) ⇒ Object



137
138
139
140
141
# File 'lib/image_svd/cli.rb', line 137

def self.process(opts)
  vs = format_num_sing_vals(opts[:num_singular_values].to_s)
  n = validate_thread_count(opts[:thread_count])
  opts.merge(singular_values: vs, thread_count: n)
end

.validate_thread_count(n) ⇒ Object

prevent non positive requested thread_counts



152
153
154
# File 'lib/image_svd/cli.rb', line 152

def self.validate_thread_count(n)
  n > 1 ? n : 1
end