Class: ZSteg::CLI::Reflow

Inherits:
Object
  • Object
show all
Defined in:
lib/zsteg/cli/reflow.rb

Constant Summary collapse

DEFAULT_ACTIONS =
%w'reflow'

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV) ⇒ Reflow

Returns a new instance of Reflow.



10
11
12
13
14
# File 'lib/zsteg/cli/reflow.rb', line 10

def initialize argv = ARGV
  @argv = argv
  @cache = {}
  @wasfiles = Set.new
end

Instance Method Details

#load_image(fname) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/zsteg/cli/reflow.rb', line 125

def load_image fname
  if File.directory?(fname)
    puts "[?] #{fname} is a directory".yellow
  else
    ZPNG::Image.load(fname)
  end
rescue ZPNG::Exception, Errno::ENOENT
  puts "[!] #{$!.inspect}".red
end

#parse_dimension(s) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/zsteg/cli/reflow.rb', line 114

def parse_dimension s
  s.split(',').map do |x|
    case x
    when /\A\d+\Z/        # single value
      x.to_i
    when /-/              # range
      Range.new(*x.split('-').map(&:to_i)).to_a
    end
  end.flatten.uniq
end

#reflowObject

actions



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
# File 'lib/zsteg/cli/reflow.rb', line 138

def reflow
  if @image.format != :bmp
    STDERR.puts "[!] only BMP format supported for now!"
    return
  end

  sl = @image.scanlines.first
  @bpp = sl.bpp
  @old_significant_sl_bytes = (sl.width*sl.bpp/8.0).ceil
  @old_total_sl_bytes       = sl.size

  if @options[:heights]
    @options[:heights].each do |h|
      t = 1.0*@image.width*@image.height/h
      t.floor.upto(t.ceil).each do |w|
        next if @options[:widths] && !@options[:widths].include?(w)
        _reflow w,h
      end
    end
  elsif @options[:widths]
    @options[:widths].each do |w|
      h = @image.width*@image.height/w
      _reflow w,h
    end
  elsif @options[:try_all]
    # enum all
    2.upto(@image.width*@image.height/2) do |w|
      h = @image.width*@image.height/w
      _reflow w,h
    end
  else
    # smart all
    w = 4
    loop do
      h = @image.width*@image.height/w
      break if h < 4
      _reflow w,h
      w += 1
    end
  end
end

#runObject



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
# File 'lib/zsteg/cli/reflow.rb', line 16

def run
  @actions = []
  @options = {
    :verbose   => 0,
  }
  optparser = OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename($0)} [options] filename.png [param_string]"
    opts.separator ""

    opts.on( "-W", "--width X", "reflow to specified width(s)",
                                "single value: '999', range: '100-200'",
                                "or comma-separated: '100,200,300-350'"
    ) do |x|
#          if @options[:heights]
#            STDERR.puts "[!] width _OR_ height can be set".red
#            exit 1
#          end
      @options[:widths] = parse_dimension(x)
    end

    opts.on "-H", "--height X", "reflow to specified height(s)" do |x|
#          if @options[:widths]
#            STDERR.puts "[!] width _OR_ height can be set".red
#            exit 1
#          end
      @options[:heights] = parse_dimension(x)
    end

    opts.separator ""

    opts.on "-a", "--all", "try all possible sizes" do
      @options[:try_all] = true
    end

    opts.on "-r", "--rewrite", "just rewrite the header, keeping imagedata as-is" do
      @options[:rewrite] = true
    end

    opts.separator ""

    opts.on "-O", "--outfile FILENAME", "output single result to specified file" do |x|
      @options[:outfile] = x
    end

    opts.on "-D", "--dir DIRNAME", "output multiple results to specified dir" do |x|
      @options[:dir] = x
    end

    opts.separator ""
    opts.on "-v", "--verbose", "Run verbosely (can be used multiple times)" do |v|
      @options[:verbose] += 1
    end
    opts.on "-q", "--quiet", "Silent any warnings (can be used multiple times)" do |v|
      @options[:verbose] -= 1
    end
    opts.on "-C", "--[no-]color", "Force (or disable) color output (default: auto)" do |x|
      if defined?(Rainbow) && Rainbow.respond_to?(:enabled=)
        Rainbow.enabled = x
      else
        Sickill::Rainbow.enabled = x
      end
    end
  end

  if (argv = optparser.parse(@argv)).empty?
    puts optparser.help
    return
  end

  @actions = DEFAULT_ACTIONS if @actions.empty?

  argv.each do |arg|
    if arg[','] && !File.exist?(arg)
      @options.merge!(decode_param_string(arg))
      argv.delete arg
    end
  end

  argv.each_with_index do |fname,idx|
    if argv.size > 1 && @options[:verbose] >= 0
      puts if idx > 0
      puts "[.] #{fname}".green
    end
    next unless @image=load_image(@fname=fname)

    @actions.each do |action|
      if action.is_a?(Array)
        self.send(*action) if self.respond_to?(action.first)
      else
        self.send(action) if self.respond_to?(action)
      end
    end
  end
rescue Errno::EPIPE
  # output interrupt, f.ex. when piping output to a 'head' command
  # prevents a 'Broken pipe - <STDOUT> (Errno::EPIPE)' message
end