Below are four C++ implementations of the region quadtree (the kind used for image compression, for example). The different implementations were made in an attempt to optimise construction of quadtrees. (For a tutorial on implementing region quadtrees, see Issue 26 [6.39 MB zip] of Dev.Mag).

• NaiveQuadtree is the straightforward implementation.
• AreaSumTableQuadtree uses a summed area table to perform fast calculations of the mean and variance of regions in the data grid.
• AugmentedAreaSumTableQuadtree is the same, except that the area sum table has an extra row and column of zeros to prevents if-then logic that slows it down and makes it tricky to understand.
• SimpleQuadtree is the same as AugmentedAreaSumTableQuadtree , except that no distinction is made (at a class level) between different node types.

The code below implements some quadtree extensions, as discussed in another Dev.Mag tutorial about quadtrees (see Issue 27). The tutorial covers the following topics:

• what to consider in choosing whether to use a quadtree or not;
• how to handle discrete data; and
• some modifications to the basic algorithm.

## Handling Discrete Data

When it comes to discrete data, the “average” of a number of pixels doesn’t make sense. However, we can give it meaning and still use it to get good approximations of the original data, as illustrated in the images below.

 The original grid of discrete data. The grid contains integers from 0 to 4. Every integer is mapped to a colour in this image. (If this grid represented a tile map, every integer would be mapped to a different tile). Here we allowed floating point numbers in the quadtree. Results of queries into the quadtree are rounded before mapping them to colours. Here we used the floating point part of queries to bias a randomly selected integer. For example, a result of 1.25 will result in a 75% chance of yielding 2. Here we use a quadtree with interpolation. The result is rounded before it is mapped. Here we use a quadtree with interpolation, and use the floating point part of the number to bias a randomly selected tile, as above.

## Python Image Code

I use this code to illustrate many of the tutorials on this site, and the articles I write for Dev.Mag. Ideally, I would like to package the code so that it is the minimal necessary for the particular tutorial; however, a lot of the code is reused, so that it becomes difficult to maintain. Instead, I distribute it all together. That way, new updates and extensions can be found in one place.

The current version includes classes and functions for:

• easy-syntax 2D and 3D arrays (for example, you can use grid[1:20:2, 2:3:20] to access the pixels in every second column (starting with column 1 and ending before column 20) and every third row (starting from row 2 and ending before row 20) (docs);
• general image utility function (docs);
• perlin noise (docs, tutorial);
• poisson-disk sampling (docs, tutorial);
• texture generation algorithms (docs, tutorial);
• quadtrees (docs, tutorial part1 and part 2);
• classes for generating random points (1D and 2D) from arbitrary distributions (docs, tutorial);
• functions for blending between images (for smooth transitions between regions in seamless tile sets) [see blend_demo.py, tutorial];  and
• functions for image quilting (under construction).
A few notes:
• The code is not optimised, and in general convenience and clarity takes precedence over speed. This code is not suitable for many applications where speed is important.
• The code will change often. At this stage I do not try to make it backwards compatible.

### Python Image Code v0.6

python_image_code_v0_6.zip (593 KB)

Requires PIL (Python Image Library).

This version includes some of the dependencies that accidentally got left behind in the previous version.

## A Simple Procedural Texture Algorithm

I am playing around with generating textures and decided to post some preliminary results. The algorithm used to create these images is simple to implement, but slow. Here is how it works:

### 1. Generate White Noise

Start off with white noise (grayscale only – colour is much too slow).

### 2. Blend with random neighbourhood pixels

Generate a new image from the old one. Every pixel in the new image is a blend between the corresponding pixel in the other image, and a randomly selected pixel in a square window around that pixel. Every point in that window can be selected with a probability that is defined in a square matrix.

This matrix determines how the texture will turn out; it is unfortunately a bit hard to guess how the texture will look given the matrix, in the general case, without some mathematical analysis.

### 3. Repeat

Repeat the above step. The more you repeat, the smoother the result is. The images below were created by repeating the step 50 times. On my computer, generating a 128 by 128 tile takes about 10 minutes (Python implementation).

### 4. Convert Grayscale to RGB

Normalise the image, and map to a gradient.

Some example textures are shown above.

## Questions

There are some things that I still want to investigate:

• Is there a way to significantly speed up the algorithm?
• Is there an intuitive way to linkthe matrix with the result?
• What are the effects of starting with something other than white noise?

## Code

The code is currently too messy to release. I have built it on top of the code that was released for the Quadtrees article, so that is a good starting point if you do not want to wait. Otherwise, keep an eye out, I should post some code soon.