-repeat … -done

Figure 1: The 1911 Goudy Bookletter  ampersand Compact Disc.

The -repeat … -done block executes any code within its precinct over a number of cycles given by -repeat's numeric argument.

The format of the command is:

-repeat iterations,loop_variable … -done

'Iteration' may take any integer value, but -repeat does not act upon zero and negative arguments, furnishing an implicit conditional test should-repeat's argument stem from computations that may produce non-positive values.

'loop_variable', an optional argument, establishes a label for a local (within the loop) variable; it holds the current loop count. Do not use the '$' substitution signet when defining the variable label, but do use '$' with the variable label in the body of the loop to reference the current count.

A programmer can terminate a -repeat … -done iteration prematurely by executing a -break command anywhere in the body of the block. Usually one embeds the -break command in a -if … -endif block. Similarly, a -continue command within the -repeat … -done block terminates the current iteration; -repeat decrements its loop count and, if that is still positive, restarts execution with the initial command in the body of the -repeat … -done block.

List Iteration with -local … -endlocal

It is a common G'MIC idiom to nest a -local-endlocal block within a -repeat … -done block; The construct creates an image list iterator, a form that many G'MIC commands take. In this construct, the argument to -repeat is frequently the image list size substitution sequence, '$!'. As -repeat iterates from zero to the size of the image list, less one, another substitution sequence '$>' assumes the loop count, incrementing by one with each iteration. This becomes the argument to -local, constituting the inner block of the iterator. This induces -local to create a 'local list' consisting of just the image indexed by $>. The body of the command resides in the -local-endlocal block, where the effects of its operations are restricted to the one image on the local list. In the larger scheme of things, the image iterator 'visits' each image in turn and during the course of the iteration the effects of its operations are limited to that one image.  

Examples

We set our course upon transforming the 1911 Goudy Bookletter ampersand into a compact disc (approximately), a desire that has been burning in our hearts since early childhood, perhaps.

$ gmic -input ampersand_goudybl1911.svg \
-negative[-1] \
-blur[-1] 3,1,0
Ampersand by Barry Schwartz who also designed a nice font to go with it. We negate and slightly blur the ampersand because slightly blurred edges behave a bit like neon light ribbons under the Negation compositng operator of the -blend command; that happens downstream from here.
100% 100% \
-append c 
 We conjure from the aether two black images and append along the spectral dimension to obtain a red, slightly blurred, ampersand.
-expand_x {{w}/2} Pad horizontally. In G'MIC, you do math within curly braces. The substitution sequences which fetch metrics about the last image on the stack also harness curly braces, giving rise, perhaps,  to a bemusement of curly braces. This particular substitution sequence fetches the width of the last image on the list.
100%,100%,100%,100% -append[-2,-1] y Pad vertically, after conjuring another image from the aether. Now we have room to twirl.
100%,100%,100%,100%  Conjure (yet another) image from the aether, this a drawing pad upon which to rubber-stamp ampersands.
-repeat {360/6} \
k={6*($>+1)}

We've worked our way up to the example on -repeat, the point to this entire program. We're stepping around the circle in six degree increments, which we will do sixty times. Why not simply write '60' for -repeat's argument? A personal taste. Writing the computation instead of the result documents the code a little bit; I am reminded that I'm dividing up a circle. Your tastes will vary.

More math within curly braces; we set 'k' to the accumulated rotation, this based on another G'MIC substitution sequence: '$>' which the interpreter replaces with the loop count.

  --rotate[0] {$k},2,1,255,255 We duplicate the original ampersand and rotate the copy in one swell foop - observe the double hyphen lefthand decoration
 -apply_channels[-1] "-add "$k,hsl_h
 What goes on here? In one swell foop, -apply channels converts the image into the HSL color space and lets us rotate the hue angle by the current increment, $k. That done, -apply channels brings us back to RGB space.
  -blend[-2,-1] negation,0.7  We merge the current ampersand onto the drawing pad using G'MIC compositing engine, the rather extensive -blend command. Negation is the blending operation
-done -rm[-2] That's it; we're done. We remove the rubber-stamping image.
-output twirl.png Save the creation for the front piece illustration.

Garry Osgood