Class: Punk::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptopunks/tool.rb

Defined Under Namespace

Classes: Opts

Instance Method Summary collapse

Instance Method Details

#run(args) ⇒ Object

use def self.run instead of run e.g.

Tool.run( ARGV ) vs Tool.new.run( ARGV )


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
126
127
128
129
130
131
132
133
134
135
136
137
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/cryptopunks/tool.rb', line 41

def run( args )  
opts = Opts.new

parser = CmdParse::CommandParser.new(handle_exceptions: :no_help)
parser.main_options.program_name = 'punk'
parser.main_options.banner = 'punk (or cryptopunk) command line tool'
parser.main_options.version = Pixelart::Module::Cryptopunks::VERSION



parser.global_options do |opt|
      opt.on( '-z', '--zoom=NUM', Integer, 
              "Zoom factor x2, x4, x8, etc. (default: #{opts.zoom})" 
            ) do |num|
             opts.zoom = num
            end

      opt.on( '--offset=NUM', Integer, 
              "Start counting at offset (default: #{opts.offset})"   
            ) do |num|
              opts.offset = num
            end     

      ## todo - move to shuffle (only used by shuffle) - why? why not?      
      opt.on( '--seed=NUM', Integer,
              "Seed for random number generation / shuffle (default: #{opts.seed})"
            ) do |num|
              opts.seed = num
            end

      # note: remove -d alias (and --dir=DIR)  why? why not?
      opt.on(  '-o', '--out=DIR', '--dir=DIR', '--outdir=DIR', 
               "Output directory (default: #{opts.outdir})"
            ) do |dir|
               opts.outdir = dir
            end 

      ### todo/check: move option to -t/--tile command only - why? why not?
      # desc "True Official Genuine CryptoPunks™ all-in-one composite image"
      ## todo: add check that path is valid?? possible?
      opt.on( '-f', '--file=FILE', 
              "All-in-one composite image (default: #{opts.file})"
            ) do |file|
              opts.file = file 
            end            

      # note - not used for now - always on
      ## todo: use -w for short form? check ruby interpreter if in use too?      
      # opt.on( '--verbose',
      #         "(Debug) Show debug messages" 
      #      ) do 
      #          opts.verbose = true
      #           #     ## LogUtils::Logger.root.level = :debug
      #        end
end


# command - f, flip 
flip = CmdParse::Command.new('flip', takes_commands: false)
flip.short_desc( "Flip (vertically) all punk characters in all-in-one punk series composite (#{opts.file})" )
flip.action do
    puts "==> reading >#{opts.file}<..."
    punks = ImageComposite.read( opts.file )

    ## note: for now always assume 24x24
    tile_width = 24
    tile_height = 24
    cols = punks.width / tile_width
    rows = punks.height / tile_height

    phunks = ImageComposite.new( cols, rows,
                                 width: tile_width,
                                 height: tile_height )

    punks.each do |punk|
       phunks << punk.flip_vertically
    end

    ## make sure outdir exits (default is current working dir e.g. .)
    FileUtils.mkdir_p( opts.outdir )  unless Dir.exist?( opts.outdir )

    ## note: always assume .png extension for now
    basename = File.basename( opts.file, File.extname( opts.file ) )
    path  = "#{opts.outdir}/#{basename}-flipped.png"
    puts "==> saving phunks flipped one-by-one by hand to >#{path}<..."

    phunks.save( path )
    puts 'Done.'
end # action


# command - s, shuffle
shuffle = CmdParse::Command.new('shuffle', takes_commands: false)
shuffle.short_desc( "Shuffle all punk characters (randomly) in all-in-one punk series composite (#{opts.file})" )
shuffle.action do 
    puts "==> reading >#{opts.file}<..."
    punks = ImageComposite.read( opts.file )

    ## note: for now always assume 24x24
    tile_width = 24
    tile_height = 24
    cols = punks.width / tile_width
    rows = punks.height / tile_height

    phunks = ImageComposite.new( cols, rows,
                                 width: tile_width,
                                 height: tile_height )

    tiles = cols * rows
    indexes = (0..tiles-1).to_a
    srand( opts.seed )     ## note: for new reset **global** random seed and use (builtin) Array#shuffle
    puts "   using random generation number seed >#{opts.seed}< for shuffle"
    indexes = indexes.shuffle

    ###
    # seed 4142 ends in [..., 7566, 828, 8987, 9777]
    #       333 ends in [..., 6067, 9635, 973, 8172]

    indexes.each_with_index do |old_index,new_index|
       puts "    ##{old_index} now ##{new_index}"
       phunks << punks[old_index]
    end

    puts "    all #{tiles} old index numbers (zero-based) for reference using seed #{opts.seed}:"
    puts indexes.inspect

    ## make sure outdir exits (default is current working dir e.g. .)
    FileUtils.mkdir_p( opts.outdir )  unless Dir.exist?( opts.outdir )

    ## note: allways assume .png extension for now
    basename = File.basename( opts.file, File.extname( opts.file ) )
    path  = "#{opts.outdir}/#{basename}-#{opts.seed}.png"
    puts "==> saving p(h)unks shuffled one-by-one by hand to >#{path}<..."

    phunks.save( path )
    puts 'Done.'
end # action


# command - t, tile
tile = CmdParse::Command.new('tile', takes_commands: false)
tile.short_desc( "Get punk characters via image tiles from all-in-one punk series composite (#{opts.file}) - for IDs use 0 to 9999" )
tile.action do |*args|
    puts "==> reading >#{opts.file}<..."
    punks = ImageComposite.read( opts.file )

    puts "    setting zoom to #{opts.zoom}x"   if opts.zoom != 1

    ## make sure outdir exits (default is current working dir e.g. .)
    FileUtils.mkdir_p( opts.outdir )  unless Dir.exist?( opts.outdir )

    args.each_with_index do |arg,index|
      punk_index = arg.to_i( 10 )  ## assume base 10 decimal

      punk = punks[ punk_index ]

      punk_name = "punk-" + "%04d" % (punk_index + opts.offset)

      ##  if zoom - add x2,x4 or such
      if opts.zoom != 1
        punk = punk.zoom( opts.zoom )
        punk_name << "@#{opts.zoom}x"
      end

      path  = "#{opts.outdir}/#{punk_name}.png"
      puts "==> (#{index+1}/#{args.size}) saving punk ##{punk_index+opts.offset} to >#{path}<..."

      punk.save( path )
    end
    puts 'Done.'
end # action


# command - g, gen, generate
#   note: shorten to gen
generate = CmdParse::Command.new('gen', takes_commands: false)
generate.short_desc( "Generate punk characters from text attributes (from scratch / zero) via builtin punk spritesheet" )
generate.action do |*args|
    puts "==> generating  >#{args.join( ' + ' )}<..."
    punk = Punk::Image.generate( *args )

    puts "    setting zoom to #{opts.zoom}x"   if opts.zoom != 1

    ## make sure outdir exits (default is current working dir e.g. .)
    FileUtils.mkdir_p( opts.outdir )  unless Dir.exist?( opts.outdir )

    punk_index = 0    ## assume base 10 decimal
    punk_name = "punk-" + "%04d" % (punk_index + opts.offset)

    ##  if zoom - add x2,x4 or such
    if opts.zoom != 1
      punk = punk.zoom( opts.zoom )
      punk_name << "@#{opts.zoom}x"
    end

    path  = "#{opts.outdir}/#{punk_name}.png"
    puts "==> saving punk ##{punk_index+opts.offset} to >#{path}<..."

    punk.save( path )
    puts 'Done.'
end # action



# command - q, query
query = CmdParse::Command.new('query', takes_commands: false)
query.short_desc( "Query (builtin off-chain) punk contract for punk text attributes by IDs - use 0 to 9999" )
query.action do |*args|
    args.each_with_index do |arg,index|
      punk_index = arg.to_i( 10 )  ## assume base 10 decimal

      puts "==> (#{index+1}/#{args.size}) punk ##{punk_index}..."

      attribute_names = CryptopunksData.punk_attributes( punk_index )
      ## downcase name and change spaces to underscore
      attribute_names = attribute_names.map do |name|
                          name.downcase.gsub( ' ', '_' )
                        end

      print "  "
      print attribute_names.join( '  ' )
      print "\n"
    end
    puts 'Done.'
end


# command - l, ls    # (NOT) list 
##  note: ls (alias) is NOT possible with cmdparse gem!!!!! - ask if workaround?
##   note: as a workaround use ls for now (not list)!!!
list = CmdParse::Command.new('ls', takes_commands: false)
list.short_desc( "List all punk archetype and attribute names from builtin punk spritesheet" )
list.action do 
    # was: generator = Punk::Image.generator
    sheet = Punk::Spritesheet.builtin

    puts "==> Archetypes"
    sheet.meta.each do |rec|
      next unless rec.archetype?

      print "  "
      print "%-30s"  % "#{rec.name} / (#{rec.gender})"
      print " - #{rec.type}"
      print "\n"
    end

    puts ""
    puts "==> Attributes"
    sheet.meta.each do |rec|
      next unless rec.attribute?

      print "  "
      print "%-30s"  % "#{rec.name} / (#{rec.gender})"
      print " - #{rec.type}"
      print "\n"
    end

    puts ""
    puts "  See github.com/openpunkart/punkart.spritesheet for more."
    puts ""

    puts 'Done.'
end # action


parser.add_command(CmdParse::HelpCommand.new, default: true)
parser.add_command(CmdParse::VersionCommand.new)
parser.add_command( flip )
parser.add_command( shuffle )
parser.add_command( tile )
parser.add_command( generate )
parser.add_command( query )
parser.add_command( list )


parser.parse( args )
end