-plasma

Plasma blends and perturbs an input image based on the Diamond-Square Algorithm producing an effect similar to hovering smoke, fog, thin altostratus cloud formations and other undulating hazes. Also see its cousin, -turbulence.

G'MIC's implementation samples, blends, and perturbs either images or constant color fields (including black), in the latter case generating cloud-like patterns. Gray scale plasma patterns are often used as height maps for three dimensional mountainous terrain.

As a sampling and blending algorithm, the command tends to preserve the local average intensity within imagery, but with a randomizing component, also introduces visual roughness. Thus plasma shares attributes with both blurring and noisemaking filters, often to singular effect.

The algorithm proceeds in discrete stages from initially coarse- to fine-scale grids, the number of such being set by the scale parameter. At each stage, the algorithm samples the existing image at the current grid intersection points, blends these data into a set of interpolated points, then randomly perturbs these, the intensity of such controlled by scale, alpha and beta (see 'How the Parameters Interact', below). The new sample points, added to those present at the start of a stage, together compose the finer-mesh sampling grid of the next stage. When the fineness of the grid reaches pixel dimensions, the algorithm ceases.

The Diamond-Square algorithm draws its name from the formation of its sampling grid, which, at a particular stage, consists of horizontally and vertically aligned cells with sides of length d, (the “squares”) and a slightly finer grid with sides d ⁄ √2 orientated at 45° (the “diamonds”). The two sampling and blending grids operate in concert to minimize the formation of horizontally and vertically aligned artifacts that would be otherwise present with grids of a single orientation. The finer grid, consisting of the side midpoints and center points of the coarser grid, combine together with the square grid to form the d /2 square grid of the next stage, from which the next stage diamond grid is derived.

The format of the command is:

plasma _alpha,_beta,_scale

  1. _alpha: Controls perturbation effects in a scale-dependent way. These decrease as the algorithm progresses from coarse to fine grids. Use a non-negative floating point value.
  2. _beta: Controls perturbation effects independent of scale. The effect of this component remains constant as the algorithm progresses. Use a non-negative floating point value
  3. _scale: Controls the size of features relative to overall image size and establishes the number of stages through which the algorithm progresses. A positive integer, _scale can range from zero to thirty one, but the useful range is dependent on the size of the input image. See 'How the Parameters Interact', below.

How the Parameters Interact

The Diamond-Square algorithm employs a combination of:

  1. averaging of grid points at one scale to arrive at the values of intermediary grid points for the next finer scale, a kind of interpolating, or blurring, effect, and
  2. perturbing the new intermediary grid point values with randomly derived offsets, these scaled by an amount set by scale, alpha and beta. A roughening effect.

Beginning with a coarse-mesh grid, the Diamond-Square algorithm progresses through a series of grids, sample-averaging and perturbing the results at ever-so-finer scales, stopping at the 1 pixel scale.

Scale sets the number of grids which the algorithm uses, and from which follows directly the coarseness of the initial grid:

d = 2s for d, length of sides of the initial grid, in pixels, and s, scale, given as a parameter.

For large scale parameters, the mesh of the initial grid is very coarse, sample points are far apart and are averaged over a wide distance. Consequently, the very first samples tend to influence pixels over a very wide area and form the basis for large features. A large scale parameter also means that the algorithm will operate over many stages, each with sample points half the distance apart from each other as the previous grid, so that details arising from random perturbations occur at many scales, giving rise to nuanced effects.

On the other hand, small scale parameters lead to initial grids that are already fine, so the range of influence that grid points have is limited and feature size in the final image is small. The number of grids that the algorithm runs through is also small, so the algorithm stops after only a few iterations, further limiting large-scale effects. A scale of one operating over a constant color field is barely distinguishable from random noise.

Scale, alpha and beta together establish the perturbation amount at each stage, i, of the algorithm, an amount that generally gets smaller as the algorithm progresses through ever finer grids, though the amount of decay depends on particular choices of the three parameters. Here are the relationships among them:

As the algorithm progresses through the grids, i, from 1 to s, it computes a perturbation offset, Pi, which it adds to each new intermediary grid point. Pi, in turn, arises from a scaled decay factor, the product of alpha, the first parameter from the command line, and a grid-dependent scale factor, di. To this component, the algorithm adds a scale independent factor, beta, the second parameter from the command line. The grid-dependent decay factor, di begins as a relatively large factor, when the initial coarse grids are in play, but it decreases by powers of two as the finer grids come into play.

  1. If both alpha and beta are zero, Pi is zero and no random perturbations take place at any scale. the Diamond-square algorithm becomes a wholly averaging mechanism.
  2. For non-zero alpha, the scale dependent part of Pi is in play. A very small alpha can throttle this scale dependent factor, just as a very large alpha can make it dominant.
  3. For non-zero beta, the scale independent part of Pi is in play. This component does not change as the algorithm progresses through the grids, it furnishes a constant kind of noise that is present at all scales, a noise that dominates for beta larger than alpha.

Plasma Scale and Image Size

The size of the image virtually places a cap on the effect of scale. A normal plasma has a near-zero, perhaps slightly positive average. That is, the minimum value of a plasma-generated image is nearly equal in magnitude to the maximum value, and of opposite sign. But as one increases scale without increasing the size of the image, there comes a point when the features in the image do not grow larger. That is because the coarsest grids are larger than the image size itself – recall that these grow by powers of two.

Instead, excessive scale shows up as an image bias. Both the image minimum and maximum grow in tandem. For really excessive values of scale, both the image minimum and maximum will be on the order of a million or ten million, yet be separated by a difference of only a hundred or so. When experimenting with plasmas, keep track of image minima and maxima: if they are not nearly equal and opposite, chances are that scale is uselessly large.

Repeating Plasmas Using -srand

From time to time, you may need to exactly repeat a particular plasma, which, at first blush, may seem well nigh impossible, given all the randomness of the Diamond-Square algorithm. The randomness, however, is of the pseudo sort, and is, in fact, a wholly deterministic process which spits out number sequences that exhibit statistical properties akin to randomness, but if you can figure out how to dive into a particular spot in the sequence – say, the i-th position – you'll notice that i + 1 is always the same. Ditto i + 2, and i + 7,593,452, for that matter.

The G'MIC command that lets you dive into a particular spot in the pseudo-random sequence is -srand, which seeds G'MIC's random number generator. Given that the random number generator is completely deterministic, feeding it a particular seed will start it off on a particular sequence and that sequence always follows from that particular seed. You can use the number that you feed to -srand as an identifier for a particular plasma. That, and a particular scale value, gives rise to a pattern which alpha and beta can modify, but not entirely obliterate.

Among other things, this aspect of -plasma furnishes a great way to investigate the effects of the various plasma parameters. Let's look at a range of beta's shall we?

gmic -repeat 5 -srand 123456 100,100,1,1 -plasma[-1] 1,'{$>}',6 -done -d -append x -n[-1] 0,255 -o[-1] betatest.png

The one bit of Mysterious Notation in this pipeline, '{$>}', is one of G'MIC's many, many substitution expressions, this one representing the current value of an overarching -repeat...-done loop. Armed with this bit, it should be fairly clear that as we loop, 0, 1, 2, 3, 4, 5 and 6 are successively substituted for the '{$>}' expression – we are running the same plasma command with the same parameters, alpha 1, scale 6, except beta, which ascends from 0 to 6 with each loop. The other matter of note is the invocation of -srand at the top of the loop, essentially conjuring up plasma number '123456', which broadly composes a particular plasma, varying only with the beta parameter. Armed with this technique you are free to investigate other parameters on your own.

Plasma is a Dataset: Pay Heed to Image Minima and Maxima

Even the most experienced G'MIC scripter will save out a pretty plasma from time to time, only to find it turning out black in Gimp or other paint programs. Plasmas are datasets! Unless scale happens to be too large for a given image size, plasmas tend to have the same average as their input images. Should the input image be dark or black, then roughly half the pixels in the image will be 'blacker than black', so far as paint programs are concerned.

`Tis generally a good idea to insert -display commands here and there and pay close attention to G'MIC's summary lines; in all likelihood, you will need to -normalize the image before passing the results to the bitmap editor of your choice. 

Examples

Plasma is both a rendering and a filtering command. If given an image, the command will blend and perturb it in one atomic operation, giving rise to results akin to applying noise to an image, and then blurring it.

But not quite akin to noise, as plasma, unlike noise, can exhibit structure at different scales and an enchanting ability to mimic traditional artistic processes, notably brush strokes on canvas. Of course, given that many traditional painters spend much of their time on developing their brush work, my association of such to mere randomness has now pretty much alienated all the traditionalists out there, leaving — oh what? — one or two digerati here and there. So now that the room has been cleared, lets look at some practical examples.

Tintern Abbey, Monmouthshire, Wales  
We change the color model to Hue-saturation-luminance and inject the same plasma-generated pattern, '781123' in each of the channels, varying the parameters. Quite apart from the plasma command, we also adjust the hue, saturation and luminance range to obtain a low saturated color image compressed into the red, yellow, yellow-green end of the color wheel. These machinations are reminiscent of the photographic Pictorialists of the early twentieth century. They played with darkroom chemistry, we play with bits. Similar ambitions, though. gmic -srand 781123
  tinternabbey_small.png
  -remove_opacity[-1] -rgb2hsl[-1]
  -split[-1] c
  -plasma[-3] 1,2,2
  -srand 781123 -plasma[-2] 0.01,0.01,3
  -srand 781123 -plasma[-1] 0.01,0.01,2
  -normalize[-3] 10,70
  -normalize[-2] 0.0,0.5
  -normalize[-1] 0.3,0.8
  -append[-3,-2,-1] c -hsl2rgb[-1]
One of the nice aspects of the multi-scale noise of plasma, is the almost always interesting response that the anisotropic smoothing command gives when it tries to trace edges through the plasma. The two commands, -plasma and -smooth working in tandem give rise to a tremendous range of pseudo brush-work.
We also made recourse to -gradient, -direction2rgb and friends to generate color accent edges, giving a sense of pseudo-depth to our pseudo brush work.
-smooth 70,0,1.0,5.0,0.5,3,3,2,2,0
  --sharpen[-1] 80 -gradient[-1]
  -append[-2,-1] c -blur[-1] 2
  -direction2rgb[-1] -*[-1] -1
  -normalize[-1] -0.1,0
  -exp[-1] -apply_channels[-1] \"-mul 0.3\",12
  -normalize[-1] 0,1 -*[-2,-1]

Garry Osgood

Image: Tintern Abbey by NotFromUtrecht, Wikimedia Commons