## Modulo patterns in C (PGM and PPM)

During one my Engineering Computing classes last semester, I was writing an example C program to create a PGM image file when I accidentally produced an interesting pattern. I had been using a simple function of the pixel coordinates x and y to quickly generate a pattern and I included a modulo operator to limit the maximum pixel value to 255. What I saw was something like the following: Here’s the complete C code I used to produce that image:

```//
// PGM modulo pattern maker - Ted Burke, 13-2-2012
//

#include <stdio.h>

int main()
{
int x, y, w=640, h=480;

// Open output PGM file
FILE* f = fopen("pattern.pgm", "w");

// Print image header to file
fprintf(f, "P2\n# batchloaf.wordpress.com\n%d %d\n255\n", w, h);

// Generate pixel values and write to file
for (y=0 ; y<h ; ++y)
{
for (x=0 ; x<w ; ++x)
{
fprintf(f, "%03d ", (x*x + y*y) % 255);
}
fprintf(f, "\n");
}

// Close file
fclose(f);

return 0;
}
```

Also, I used the following ImageMagick command to convert the image to PNG format before uploading it here:

`convert pattern.pgm pattern.png`

Over the weekend, I spent a couple of hours playing around with the same idea and produced some wild patterns. This time, I wanted to produce colourful patterns so I created PPM files instead. I was really surprised by how rich the patterns produced by such simple formulae are. Here’s an example: Here’s the code I used to produce the image above:

```//
// pattern.c - a PPM modulo pattern maker
// Written by Ted Burke, 13-2-2012
//
// To compile: gcc pattern.c -lm
//

#include <stdio.h>
#include <math.h>

int main()
{
int r, g, b, x, y, w=640, h=480;

// Open output PPM file
FILE* f = fopen("pattern.ppm", "w");

// Print image header to file
fprintf(f, "P3\n# batchloaf.wordpress.com\n%d %d\n255\n", w, h);

// Generate pixel values and write to file
for (y=0 ; y<h ; ++y)
{
for (x=0 ; x<w ; ++x)
{
r = fmod(0.001*(pow(x-w/2,3) + pow(y-w/2,3)),255.0);
g = fmod(0.001*(pow(3*w/2-x,3) + pow(3*h/2-y,3)),255.0);
b = 255;
fprintf(f, "%03d %03d %03d ", r, g, b);
}
fprintf(f, "\n");
}

// Close file
fclose(f);

return 0;
}
```

Finally, here are some more examples:   The next one uses a sine function rather than the modulo operator to clamp the pixel value to the allowed range (0 to 255):

```r = sin(0.025*(pow(x,1.1) * pow(y,1.1))) * 255.0;
g = (255.0 * x)/w;
b = (255.0 * y)/h;``` This entry was posted in Uncategorized and tagged , , , , , , , , , , . Bookmark the permalink.

### 2 Responses to Modulo patterns in C (PGM and PPM)

1. abdallagafar says:

I am guessing this work is closely related to the voronoi patterns studied by Craig Kaplan.
Please refer to his publication here:

Click to access kaplan_bridges2005a.pdf

• batchloaf says:

Well, my images don’t really have anything to do with Voronoi diagrams as such, but that guy’s images certainly seem to arise from a similar phenomenon as mine do. They’re all basically just aliased images of some kind of modulo function, so they tend to pop up by accident quite frequently.