From d16c6131edafb647bfb3e1604f4ea53375990d73 Mon Sep 17 00:00:00 2001 From: Daniel Sperl Date: Tue, 6 Dec 2016 09:28:02 +0100 Subject: [PATCH] added 'auto-size' option to field agent --- util/field_agent/README.md | 18 +++++++++++++----- util/field_agent/field_agent.rb | 15 +++++++++++---- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/util/field_agent/README.md b/util/field_agent/README.md index aac294684..e45d3e3fc 100644 --- a/util/field_agent/README.md +++ b/util/field_agent/README.md @@ -9,7 +9,7 @@ This allows them to be scaled to almost any size while staying crisp and sharp. This tool is actually a short [Ruby][2] script that calls [ImageMagick][3] to create the distance field. So you need both Ruby and ImageMagick installed on your computer. -### MacOS +### macOS MacOS already comes with Ruby, so you only need to install ImageMagick. For that, my recommendation is to first install [Homebrew][4], a fantastic package manager for macOS. @@ -17,7 +17,7 @@ Once ready, you can install ImageMagick (like many other tools and libraries) wi brew install imagemagick -You also optionally make the script executable, which allows you to call it directly (without passing it to Ruby). +You can also optionally make the script executable, which will allow you to call it directly (without passing it to Ruby). cd /path/to/starling/util/field_agent chmod u+x field_agent.rb @@ -31,6 +31,8 @@ Once ready, install both Ruby and ImageMagick via the command line (`cmd.exe`). choco install ruby choco install imagemagick +It also works great with [Cygwin][6]. + ## Usage As input, the "Field Agent" requires a pure black and white bitmap. @@ -69,10 +71,15 @@ Of course, you can customize that: Note that the value is given in pixels of the _output_ image. -### Rendering +### More options + +Run the script without any arguments to see a list of all supported options. +For example, it also supports trimming the texture to the optimal size or inverting its colors. + +## Using the Texture in Starling Inside Starling, load the distance field texture as usual, then assign it to an _Image_ or any other kind of _Mesh_. -To switch to distance field rendering, simply assign the appropriate [style][6]. +To switch to distance field rendering, simply assign the appropriate [style][7]. var image:Image = new Image(distanceFieldTexture); image.style = new DistanceFieldStyle(); @@ -84,4 +91,5 @@ That's it! [3]: http://www.imagemagick.org [4]: http://brew.sh [5]: https://chocolatey.org -[6]: http://doc.starling-framework.org/current/starling/styles/DistanceFieldStyle.html +[6]: https://cygwin.com +[7]: http://doc.starling-framework.org/current/starling/styles/DistanceFieldStyle.html diff --git a/util/field_agent/field_agent.rb b/util/field_agent/field_agent.rb index 18bb978e9..4a505f951 100755 --- a/util/field_agent/field_agent.rb +++ b/util/field_agent/field_agent.rb @@ -14,8 +14,8 @@ input_file = nil output_file = nil -def windows? - (/mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil +def windows_prompt? + `echo \\(`.strip != "(" end def log(message) @@ -29,7 +29,7 @@ def fail(message) # crude workaround so that the call to 'convert' works on Windows, too. def system_xp(command) - if windows? + if windows_prompt? command.gsub!("'", '"') command.gsub!("%", "%%") command.gsub!("\\(", "(") @@ -43,7 +43,8 @@ def system_xp(command) options.spread = 8 options.quality = 1 options.scale = 1 -options.convert_path = windows? ? 'magick' : 'convert' +options.auto_size = false +options.convert_path = windows_prompt? ? 'magick' : 'convert' option_parser = OptionParser.new do |opts| opts.banner = "Usage: #{File.basename(__FILE__)} input-file output-file [options]" @@ -66,6 +67,10 @@ def system_xp(command) options.invert = true end + opts.on('-a', '--auto-size', 'Extend or trim image to the size required by the DF.') do + options.auto_size = true + end + opts.on('-c', '--convert-path PATH', String, 'Name or path of the ImageMagick "convert" tool.') do |path| options.convert_path = path end @@ -104,9 +109,11 @@ def system_xp(command) command << "-negate " if options.invert command << "-background white -alpha remove " command << "-filter Jinc -resize #{options.quality * 100}\% -threshold 30% " unless options.quality == 1 +command << "-bordercolor white -border #{spread}x#{spread} " if options.auto_size command << "\\( +clone -negate -morphology Distance Euclidean:4,'#{spread}!' -level 50%,-50% \\) " command << "-morphology Distance Euclidean:4,'#{spread}!' " command << "-compose Plus -composite " +command << "-trim " if options.auto_size command << "-resize #{options.scale / options.quality * 100}\% " unless options.scale * options.quality == 1 command << "-negate -alpha copy -channel RGB -fx '#fff' " command << "PNG32:#{output_file}"