Skip to content

Instantly share code, notes, and snippets.

@DieHertz
Last active December 25, 2015 14:29
Show Gist options
  • Save DieHertz/6991085 to your computer and use it in GitHub Desktop.
Save DieHertz/6991085 to your computer and use it in GitHub Desktop.
Generates pretty images :-)
import std.stdio : File, fwrite;
import std.math : sqrt;
struct Header {
align (1):
short bitmap = 0x4d42;
long fileSize;
int headerSize = this.sizeof;
int dataOffset = 40;
int width;
int height;
short planes = 1;
short depth = 24;
int compression = 0;
int bitmapSize;
int unknown4;
int unknown5;
int unknown6;
int unknown7;
}
align (1) struct Pixel {
align (1):
byte r;
byte g;
byte b;
}
void main(string[] args) {
auto f = File("image.bmp", "w");
const int width = 1024;
const int height = 1024;
const int centerJ = width / 2;
const int centerI = height / 2;
Pixel[height][width] image;
for (int i = 0; i < height; ++i) {
for (int j = 0; j < height; ++j) {
const int distanceI = i - centerI;
const int distanceJ = j - centerJ;
const float pixelDistanceSq = distanceI * distanceI + distanceJ * distanceJ;
const float pixelSizeSq = width * width + height * height;
const float distance = 4 * sqrt(pixelDistanceSq) / sqrt(pixelSizeSq);
image[height - i - 1][j].r = cast(byte) (255 * (distance + cast(float) j / width));
image[height - i - 1][j].g = cast(byte) (255 * (distance + 1 - sqrt(cast(float) (j * j + i * i) / (width * width + height * height))));
image[height - i - 1][j].b = cast(byte) (255 * (distance + cast(float) i / height));
}
}
Header hdr = {
fileSize : Header.sizeof + image.sizeof,
width : width,
height : height,
bitmapSize: image.sizeof
};
fwrite(&hdr, hdr.sizeof, 1, f.getFP());
fwrite(&image, image.sizeof, 1, f.getFP());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment