// Extremely simple fault noise image generator // Written by Jarmo (http://z0b.kapsi.fi) // Public domain. No warranty of any kind, use at your own risk. // Compile with: gcc -s -O2 -Wall -o fault[.exe] fault.cpp -std=c99 #include #include #include #include // adjustable parameters #define WIDTH 512 #define HEIGHT 512 #define NUM_ITERATIONS 4096 #define RANDOM_SEED 239398 // returns a random number on range [min, max] static int randRange(const int min, const int max) { return rand() % (max - min + 1) + min; } int main() { uint8_t *noise = malloc(WIDTH * HEIGHT * sizeof(uint8_t)); if (!noise) { printf("Out of memory\n"); return 1; } // initial value is "in the middle" memset(noise, 128, WIDTH * HEIGHT); srand(RANDOM_SEED); printf("Generating noise...\n"); for (size_t i = 0; i < NUM_ITERATIONS; i++) { // create a random line const float x1 = randRange(0, WIDTH - 1), x2 = randRange(0, WIDTH - 1), y1 = randRange(0, HEIGHT - 1), y2 = randRange(0, HEIGHT - 1); // compute a normal for it (no need to normalize this) const float nx = y2 - y1, ny = -(x2 - x1); uint8_t *dst = noise; for (size_t y = 0; y < HEIGHT; y++) { for (size_t x = 0; x < WIDTH; x++) { // dot product between the point and the line normal // tells us how far the point is from the line const float dist = (x - x1) * nx + (y - y1) * ny; if (dist < 0.0f) { // decrement on the back side if (*dst > 0) (*dst)--; } else { // increment on the front side if (*dst < 255) (*dst)++; } ++dst; } } } // save the image in TGA format printf("Saving the image...\n"); FILE *f = fopen("fault_noise.tga", "wb"); if (!f) { printf("Could not open the output file\n"); free(noise); return 1; } const uint8_t header[18] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, WIDTH & 0xFF, WIDTH >> 8, HEIGHT & 0xFF, HEIGHT >> 8, 24, 1 << 5 }; fwrite(header, 1, 18, f); const uint8_t *p = noise; for (size_t i = 0, j = WIDTH * HEIGHT; i < j; i++) { fputc(*p, f); fputc(*p, f); fputc(*p, f); p++; } fclose(f); free(noise); printf("Done\n"); return 0; }