# Fractal flame

Fractal flames are a member of the iterated function system class of fractals created by Scott Draves in 1992. Draves' open-source code was later ported into Adobe After Effects graphics software and translated into the Apophysis fractal flame editor.
Fractal flames differ from ordinary iterated function systems in three ways:
* Nonlinear functions are iterated in addition to affine transforms.
* Log-density display instead of linear or binary (a form of tone mapping)
* Color by structure (i.e. by the recursive path taken) instead of monochrome or by density.
The tone mapping and coloring are designed to display as much of the detail of the fractal as possible, which generally results in a more aesthetically pleasing image.

## Algorithm

The algorithm consists of two steps: creating a histogram and then rendering the histogram.

### Creating the histogram

First, one iterates a set of functions, starting from a randomly chosen point P = (P.x,P.y,P.c), where the third coordinate indicates the current color of the point.
    Set of flame functions: ${\begin{cases}F_{1}(x,y),\quad p_{1}\\F_{2}(x,y),\quad p_{2}\\\dots \\F_{n}(x,y),\quad p_{n}\end{cases}}$
In each iteration, choose one of the functions above where the probability that F$_j$ is chosen is p$_j$. Then one computes the next iteration of P by applying F$_j$ on (P.x,P.y).
Each individual function has the following form:
    $F_{j}(x,y)=\sum _{V_{k}\in Variations}w_{k}\cdot V_{k}(a_{j}x+b_{j}y+c_{j},d_{j}x+e_{j}y+f_{j})$
where the parameter w$_k$ is called the weight of the variation V$_k$. Draves suggests that all $w_{k}$:s are non-negative and sum to one, but implementations such as Apophysis do not impose that restriction.
The functions V$_k$ are a set of predefined functions. A few examples are
* V$_0$(x,y) = (x,y) (Linear)
* V$_1$(x,y) = (sin x,sin y) (Sinusoidal)
* V$_2$(x,y) = (x,y)/(x$^2$+y$^2$) (Spherical)
The color P.c of the point is blended with the color associated with the latest applied function F$_j$:
    P.c := (P.c + (F$_j$)$_{color}$) / 2
After each iteration, one updates the histogram at the point corresponding to (P.x,P.y). This is done as follows:
The colors in the image will therefore reflect what functions were used to get to that part of the image.

### Rendering an image

To increase the quality of the image, one can use supersampling to decrease the noise. This involves creating a histogram larger than the image so each pixel has multiple data points to pull from. For example, create a histogram with 300×300 cells in order to draw a 100×100 px image; each pixel would use a 3×3 group of histogram buckets to calculate its value.
For each pixel (x,y) in the final image, do the following computations:
The algorithm above uses gamma correction to make the colors appear brighter. This is implemented in for example the Apophysis software.
To increase the quality even more, one can use gamma correction on each individual color channel, but this is a very heavy computation, since the log function is slow.
A simplified algorithm would be to let the brightness be linearly dependent on the frequency:
but this would make some parts of the fractal lose detail, which is undesirable.

## Density Estimation

The flame algorithm is like a Monte Carlo simulation, with the flame quality directly proportional to the number of iterations of the simulation. The noise that results from this stochastic sampling can be reduced by blurring the image, to get a smoother result in less time. One does not however want to lose resolution in the parts of the image that receive many samples and so have little noise.
This problem can be solved with adaptive density estimation to increase image quality while keeping render times to a minimum. FLAM3 uses a simplification of the methods presented in *Adaptive Filtering for Progressive Monte Carlo Image Rendering*, a paper presented at WSCG 2000 by Frank Suykens and Yves D. Willems. The idea is to vary the width of the filter inversely proportional to the number of samples available.
As a result, areas with few samples and high noise become blurred and smoothed, but areas with many samples and low noise are left unaffected.
Not all Flame implementations use density estimation.