Module: NSWTopo::Dither

Included in:
Map
Defined in:
lib/nswtopo/dither.rb

Constant Summary collapse

Missing =
Class.new RuntimeError

Instance Method Summary collapse

Instance Method Details

#dither(*png_paths) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nswtopo/dither.rb', line 5

def dither(*png_paths)
  Enumerator.new do |yielder|
    yielder << -> { OS.pngquant *%w[--quiet --force --ext .png --speed 1 --nofs], *png_paths }
    gimp_script = <<~EOF
      (map
        (lambda (path)
          (let*
            (
              (image (car (gimp-file-load RUN-NONINTERACTIVE path path)))
              (drawable (car (gimp-image-get-active-layer image)))
            )
            (gimp-image-convert-indexed image FSLOWBLEED-DITHER MAKE-PALETTE 256 FALSE FALSE "")
            (gimp-file-save RUN-NONINTERACTIVE image drawable path path)
          )
        )
        (list "#{png_paths.join ?\s}")
      )
    EOF
    yielder << -> { OS.gimp "-c", "-d", "-f", "-i", "-b", gimp_script, "-b", "(gimp-quit TRUE)" }
    yielder << -> { OS.magick *%w[mogrify -type PaletteBilevelAlpha -dither Riemersma -colors 256], *png_paths }
    raise Missing, "pngquant, GIMP or ImageMagick required for dithering"
  end.each do |dither|
    dither.call
    break
  rescue OS::Missing
  end
end