LISP &#8212; The Language That Will Not Die
<p>I&#8217;ve spent large parts of the last week editing maps for a game<br />
system I&#8217;m working on.  I&#8217;ve been using the <a href='http://www.gimp.org'>GIMP</a> graphics editor, and I&#8217;m pretty<br />
impressed with it.  I haven&#8217;t found anything I can&#8217;t easily make it do<br />
&mdash; except, oddly enough, draw straight lines between defined<br />
endpoints.  (I suspect there&#8217;s actually a way to do this using the<br />
path facility.)</p>
<p>I have a requirement to prepare about six different variants of a<br />
base map, using the same topographic map but with different<br />
arrangements of national borders.  I&#8217;ve handled this by creating a<br />
multi-layered XCF file with the topo map as the background and the<br />
different borders as optional overlays.</p>
<p>OK, so I save the variants to flat PNGs by hand whenever I change the<br />
image, but that&#8217;s a pain.  What I wanted was a way to put in my makefile<br />
instructions that say, for each variant map, that it depends on the XCF<br />
and the way to make it is to composite a particular selected subset of layers<br />
by running GIMP in batch mode.</p>
<p>Fortunately, GIMP has an embedded Scheme interpreter that&#8217;s good<br />
for exactly this kind of thing.  Looking at some Python-Fu code by<br />
Carol Spears taught me enough about the API to get started; the fact<br />
that I&#8217;m an old LISP head got me the rest of the way.</p>
<p>Here it is.</p>
<pre>
;; Batch-mode select and save of a layer set as a PNG.
;; Has to be copied into  ~/.gimp-2.2/scripts to work
;;
;; Note: This assumes that gimp-drawable-get-name returns a list with
;; the actual string name as its car.  This is what gimp-2.2 does, but
;; not what the documentation says it should do!

(define (layer-set-saver infile select outfile)
   (let* ((image (car (gimp-file-load RUN-NONINTERACTIVE infile infile)))
	  (layers (cadr (gimp-image-get-layers image)))
	  (ind 0))
     (while (< ind (length layers))
	    (let* ((layer (aref layers ind))
		   (layer-name (car (gimp-drawable-get-name layer))))
	      (gimp-drawable-set-visible layer (if (member layer-name select) 1 0)))
	    (set! ind (+ ind 1)))
     (file-png-save-defaults RUN-NONINTERACTIVE 
		     image 
		     (car (gimp-image-flatten image)) 
		     outfile outfile)
     )
   (gimp-quit 0)
   )
</pre>
<p>Here's one of my makefile productions. The second arg is a list of layer names.</p>
<pre>
basic.png: basic.xcf
	gimp -i -b '(layer-set-saver "basic.xcf" (quote ("topographic" "skinny-borders" "grey-switzerland")) "basic.png")'
</pre>
<p>Apologies for the long line.</p>
<p>LISP truly is The Language That Will Not Die. And that&#8217;s a good thing.</p>
