G'MIC - GREYC's Magic for Image Computing: A Full-Featured Open-Source Framework for Image Processing
GREYC CNRS ENSICAEN UNICAEN

A Full-Featured Open-Source Framework for Image Processing



Latest stable version: 3.3.5        Current pre-release: 3.3.6 (2024/04/12)

Tutorial

repeat … done



ampersandcdThe -repeat <repeat block> -done commands form a syntactical unit that together define a repeat block of zero or more commands. -repeat takes a mathematical expression parameter that typically reduces to a repetition count, setting the number of times that the repeat block executes.

The parameter may resolve to any integer value, but the repeat block never executes when such is non-positive, an implicit conditional test.

The break command exits -repeat … -done blocks before the count completes. Usually one embeds the -break command in an iffi conditional block.

Similarly, a continue command within -repeat … -done blocks restarts script flow at the beginning of the repeat block, incrementing the loop count as if the repeat block fully executed.
Examples
1.    -repeat 5 count=$>
    -echo "The count is "$count"."
-done
Count to four from zero.
2.    -repeat 100 count=$>
    bigu={u}
    -if $bigu>0.9
       break
    -fi
-done
-echo "Big U: "$bigu" at count "$count" broke things. :("
Break on Big U.
3.    -repeat 100 count=$>
    theu={u}
    -if $theu<0.1
      -echo "The U "$theu": too small to continue..."
      -continue
    -elif $theu>0.9
      -echo "The U "$theu": too big to continue..."
      -continue
    -fi
    -echo  "The U "$theu": is just right!"
    -break
-done
-echo "Done with The U: "$theu" at count "$count". :)"
Operate only in the comfort zone.

Twirling an 1911 Goudy Ampersand
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. Odd, that, As the writer's childhood pre-dates compact discs by more than several decades. Ah, anachronisms, but we digress.

At the heart of the script, we "step-and-repeat" an ampersand image onto an accumulator through the blend Negation function, a favorite of ours. Details follow.

images/ampersand.png
-to_rgb.
-fill. 'c==0?255*(1-(i/255)):0'
-blur. 3,1,0
-expand_x {w/2}
-input 100%,100%,100%,100%
-append[-2,-1] y
Ampersand by Barry Schwartz who also designed a nice Goudy font to go with it. We:
   (1) convert the ampersand from a gray to an RGB format,
   (2) reverse the ampersand, and
   (3) remove all but the red channel, leaving that slightly blurred.
These steps prepare for a neon light ribbons effect, courtesy of the Negation compositing function. We pad the image out to give the ampersand room to twirl around the 50%, 50% image centerpoint.
+store[0] ampersand 
-name. accumulator
`
A copy of the ampersand automatically serves as the first image in the upcoming "step-and-repeat" exercise. We name it accumulator, as it will receive further stamped ampersand copies during the step and repeat. store places an off-image-list copy of ampersand into an image storage variable. This image storage variable furnishes fresh copies of the ampersand during the step-and-repeat.
-repeat {360/6} step=$>We wish to stamp one ampersand every 6°, 60 stampings all told. Why not simplify the math expression — just write 60? Could. We mean, whatever floats peoples' boats, you know? We like the extra documentation; the round-about way signals a certain intent.
    rotate={6*($step+1)}We compute rotate, the rotation, in degrees, that each ampersand copy need to turn before stamping. With each repeat loop, $step resolves to the current loop count, so we increment by six degree steps.
    -input[0] $ampersand
    -name[0] ampersand
We fetch a copy of the ampersand image from the off-list named variable, $ampersand into which the image was safe-kept upstream from here. Reinsertion uses the semantics of the input command. Tagging images via name may be over-kill for this small repeat block, but we like the extra documentation that named images affords. Names remind us of purposes and intents.
    -rotate[ampersand] {$rotate},2,1,50%,50%
    -normalize[ampersand] 0,255
We harness the rotate command to step the ampersand image around the center of the image. This is a rotation of image pixels, and in principle, there is no operation here that changes pixel intensity, but in practice, a degree of re-interpolation takes place, so pixels may swing to (slight!) negative values, or assume values (slightly!) greater than 255 — however slight, outside the range that unsigned eight bit integers can accommodate. This preoccupation matters because blend is a compositor designed for this specific bit depth. Prudence, then, dictates a renormalization to the range in which eight bit unsigned integers reside.
    -fill[ampersand] ">
                 wv=vector3(1/sqrt(3));
                 rot(wv,($step)°)*I"

                
We undertake a rotation of an entirely different character: that of color space vectors to effect color shifts

fill takes for a parameter a Pixel Processor. The pixel processor operates on every pixel in ampersand and alters its color by a color space rotation. Don't confuse color space rotations with the earlier rotation of the image per se. In color space rotation, we model colors as vectors in a three dimensional space, one which has red, green and blue unit axes.

The color space rotations here steps ampersand's color around the white axis, leaving wholly and nearly unsaturated pixels unchanged, but repointing more staturated colors to new (color space) orientations. With each step, the (initially red) pixels progressively repoint to orange, yellow, green, blue, purple, indigo and then back to red.
    -cut[ampersand] 0,255Color space can also generate vectors that are out-of-band with respect to unsigned, eight bit image channels. Here, we used cut instead of normalize. What is the difference? With -cut, pixel channel values falling below the lower bound are set to that boundary value. Similarly, pixel channel values above the upper bound are set to that value. The upside is that differences among intermediary values do not change, so channel values and the colors they form are undisturbed. The downside is that details conveyed by out of band channel values will be flattened to one or the other boundary and be lost. On the other hand, the G'MIC normalize command may be harnessed to adjust minimum and maximum values to match the minimum and maximum values of the destination file format. The channel values stored in the file will be scaled to fit within the lower and upper boundaries which will change tonal and color relationships. See Images as Data Sets.
    -blend[ampersand,accumulator]
       negation,0.985
blend is a channel compositor designed for gray (G), gray-with-opacity, (GA), red-green-blue (RGB), and RGB-with-opacity (RGBA) imagery with unsigned integer, eight-bit data channels; it hosts about sixty compositing functions and is akin to layer mode operators in Gimp and similar paint programs.


i^{\prime} = 255 - \lvert (i_{0} + i_{1}) - 255 \rvert

(0,25,50,75,100^0,149,0,149,0) -rbf. 100 nm. hill (0,25,50,75,100^149,70,0,70,149) -rbf. 100 nm. vale +display_graph[hill] 450,450,2,0,0,255,0,255,"Image 0 x","Image 0 y" +display_graph[vale] 450,450,2,0,0,255,0,255,"Image 1 x","Image 1 y" -blend[hill] [vale],negation -display_graph[hill] 450,450,2,0,0,255,0,255,"Negation x","Negation y" -mv[0] {$!} -rm[0]

 Negation takes the absolute of an offsetted sum of two images and differs that with the same offset. The results tend to zero where both images tend to zero or their maximums. The results tend to maximums where the average of the two images is near mid-tone. To those who've developed black and white prints by hand, in some antediluvian era when digital cameras were naught, the results of Negation can imitate Sabattier's effect, a.k.a. "(pseudo)solarization" the effect of briefly flashing the developing print with white light about halfway through the process.

Back in the digital realm, we reduce the opacity of the accumulating image ever so slightly. The transparency accumulates; the earlier ampersands fade with respect to the later as the blending repeats.

   -name. accumulator                       The surviving image of the blend retains the ampersand name; we rename it back to accumulator in preparation for the next round of blending.
    -normalize[accumulator] 0,255Prudence, again, dictates a renormalization of the blended images, also in preparation for the next round of blending.
-doneThe effect completes.
List Iteration with localdone
It is a common G'MIC idiom to nest a -local…-done 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-done 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.
Command Reference
$ gmic -h repeat

repeat (+):
nb_iterations

Start 'nb_iterations' iterations of a 'repeat...done' block.
'nb_iterations' is a mathematical expression that will be evaluated.

Example:
[#1] image.jpg split y repeat $! n=$> shift[$n] $<,0,0,0,2 done append y
[#2] image.jpg mode3d 2 repeat 4 imagecube3d rotate3d 1,1,0,40 snapshot3d 400,1.4 done

Tutorial: https://gmic.eu/oldtutorial/_repeat
Updated: 06-March-2022 19:37 UTC Commit:346946c694fe
G'MIC - GREYC's Magic for Image Computing: A Full-Featured Open-Source Framework for Image Processing

G'MIC is an open-source software distributed under the CeCILL free software licenses (LGPL-like and/or
GPL-compatible). Copyrights (C) Since July 2008, David Tschumperlé - GREYC UMR CNRS 6072, Image Team.