Class: ZPNG::CLI

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

Constant Summary collapse

ACTIONS =
{
  'chunks'    => 'Show file chunks (default)',
  %w'i info'      => 'General image info (default)',
  'ascii'     => 'Try to display image as ASCII (works best with monochrome images)',
  'scanlines' => 'Show scanlines info',
  'palette'   => 'Show palette'
}
DEFAULT_ACTIONS =
%w'info chunks'

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV) ⇒ CLI

Returns a new instance of CLI.



17
18
19
# File 'lib/zpng/cli.rb', line 17

def initialize argv = ARGV
  @argv = argv
end

Instance Method Details

#asciiObject



122
123
124
# File 'lib/zpng/cli.rb', line 122

def ascii
  puts @img.to_s
end

#chunksObject



118
119
120
# File 'lib/zpng/cli.rb', line 118

def chunks
  @img.dump
end

#crop(geometry) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/zpng/cli.rb', line 99

def crop geometry
  unless geometry =~ /\A(\d+)x(\d+)\+(\d+)\+(\d+)\Z/i
    STDERR.puts "[!] invalid geometry #{geometry.inspect}, must be WxH+X+Y, like 100x100+10+10"
    exit 1
  end
  @img.crop! :width => $1.to_i, :height => $2.to_i, :x => $3.to_i, :y => $4.to_i
  print @img.export unless @actions.include?(:ascii)
end

#extract_chunk(id) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/zpng/cli.rb', line 82

def extract_chunk id
  @img.chunks.each do |chunk|
    if chunk.idx == id
      case chunk
      when ZPNG::Chunk::ZTXT
        print chunk.text
      else
        print chunk.data
      end
    end
  end
end

#infoObject



112
113
114
115
116
# File 'lib/zpng/cli.rb', line 112

def info
  puts "[.] image size #{@img.width || '?'}x#{@img.height || '?'}"
  puts "[.] uncompressed imagedata size = #{@img.imagedata.size} bytes"
  puts "[.] palette = #{@img.palette}" if @img.palette
end

#load_file(fname) ⇒ Object



108
109
110
# File 'lib/zpng/cli.rb', line 108

def load_file fname
  @img = ZPNG::Image.new fname
end

#paletteObject



130
131
132
133
134
135
# File 'lib/zpng/cli.rb', line 130

def palette
  if @img.palette
    pp @img.palette
    Hexdump.dump @img.palette.data, :width => 6*3
  end
end

#runObject



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

def run
  @actions = []
  @options = { :verbose => 0 }
  optparser = OptionParser.new do |opts|
    opts.banner = "Usage: zpng [options] filename.png"

    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

    ACTIONS.each do |t,desc|
      if t.is_a?(Array)
        opts.on *[ "-#{t[0]}", "--#{t[1]}", desc, eval("lambda{ |_| @actions << :#{t[1]} }") ]
      else
        opts.on *[ "-#{t[0].upcase}", "--#{t}", desc, eval("lambda{ |_| @actions << :#{t} }") ]
      end
    end

    opts.on "-E", "--extract-chunk ID", "extract a single chunk" do |id|
      @actions << [:extract_chunk, id.to_i]
    end
    opts.on "-U", "--unpack-imagedata", "unpack Image Data (IDAT) chunk(s), output to stdout" do
      @actions << :unpack_imagedata
    end

    opts.on "-c", "--crop GEOMETRY", "crop image, {WIDTH}x{HEIGHT}+{X}+{Y},",
    "puts results on stdout unless --ascii given" do |x|
      @actions << [:crop, x]
    end
  end

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

  @actions = DEFAULT_ACTIONS if @actions.empty?

  argv.each_with_index do |fname,idx|
    @need_fname_header = (argv.size > 1)
    @file_idx  = idx
    @file_name = fname

    @zpng = load_file 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

#scanlinesObject



126
127
128
# File 'lib/zpng/cli.rb', line 126

def scanlines
  pp @img.scanlines
end

#unpack_imagedataObject



95
96
97
# File 'lib/zpng/cli.rb', line 95

def unpack_imagedata
  print @img.imagedata
end