Class: ZSteg::CLI::Mask

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

Constant Summary collapse

DEFAULT_ACTIONS =
%w'mask'
COMMON_MASKS =
[
  0b0000_0001, 0b0000_0011, 0b0000_0111, 0b0000_1111,
               0b0000_0010, 0b0000_0100, 0b0000_1000,
  0b0001_0000, 0b0010_0000, 0b0100_0000, 0b1000_0000,
]
CHANNELS =
[:r, :g, :b, :a]

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV) ⇒ Mask

Returns a new instance of Mask.



17
18
19
20
21
# File 'lib/zsteg/cli/mask.rb', line 17

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

Instance Method Details

#load_image(fname) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/zsteg/cli/mask.rb', line 141

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

#maskObject

actions



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

def mask
  masks = @options[:masks]
  masks.each{ |k,v| v.flatten!; v.uniq! }

  if @options[:try_all]
    # try all common masks
    masks = masks[:all] || []
    masks = COMMON_MASKS if masks.empty?
    masks.each{ |x| run_masker x,x,x,x    }
    masks.each{ |x| run_masker x,0,0,0xff }
    masks.each{ |x| run_masker 0,x,0,0xff }
    masks.each{ |x| run_masker 0,0,x,0xff }
    if @image.alpha_used?
      masks.each{ |x| run_masker 0,0,0,x    }
    end

  elsif CHANNELS.all?{ |c| !masks[c] || masks[c].empty? }
    # no specific channels
    masks[:all].each do |x|
      run_masker x,x,x,x
    end

  else
    # specific channels
    CHANNELS.each{ |x| masks[x] = [x==:a ? 0xff : 0] if !masks[x] || masks[x].empty? }
    masks[:r].each do |r|
      masks[:g].each do |g|
        masks[:b].each do |b|
          if @image.alpha_used?
            masks[:a].each do |a|
              run_masker r,g,b,a
            end
          else
            run_masker r,g,b,0xff
          end
        end
      end
    end
  end
end

#parse_mask(x) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/zsteg/cli/mask.rb', line 127

def parse_mask x
  case x
  when /0x/i
    x.to_i(16)
  when /^[01]{8}$/
    x.to_i(2)
  when /^\d{1,3}$/
    x.to_i
  when /^all$/
    COMMON_MASKS
  else raise "invalid mask #{x.inspect}"
  end
end

#runObject



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

def run
  @actions = []
  @options = {
    :verbose   => 0,
    :masks     => Hash.new{|k,v| k[v] = [] },
    :normalize => true
  }
  optparser = OptionParser.new do |opts|
    opts.banner = "Usage: zsteg-mask [options] filename.png [param_string]"
    opts.separator ""

    opts.on("-m", "--mask M", "apply mask to all channels",
            "mask: 0-255 OR 0x00-0xff OR 00000000-11111111",
            "OR 'all' for all common masks"
    ){ |x| @options[:masks][:all] << parse_mask(x) }

    opts.on("-R", "--red M", "red channel mask"){ |x|
      @options[:masks][:r] << parse_mask(x) }

    opts.on("-G", "--green M", "green channel mask"){ |x|
      @options[:masks][:g] << parse_mask(x) }

    opts.on("-B", "--blue M", "blue channel mask"){ |x|
      @options[:masks][:b] << parse_mask(x) }

    opts.on("-A", "--alpha M", "alpha channel mask"){ |x|
      @options[:masks][:a] << parse_mask(x) }

    opts.separator ""

    opts.on "-a", "--all", "try all common masks (default)" do
      @options[:try_all] = true
    end

    opts.separator ""

    opts.on "-N", "--[no-]normalize", "normalize color value after applying mask",
      "(default: normalize)" do |x|
      @options[:normalize] = x
    end

    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

  # default :all mask if none specified
  if @options[:masks].empty?
    @options[:try_all] = true
  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