Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* https://redd.it/a3yhkf */
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define BACKGROUND 0x202020UL
- #define WIDTH 14
- #define HEIGHT 16
- #define PAD_WIDTH 17
- #define PAD_HEIGHT 20
- /* Run-length encoding of coat of arms templates
- *
- * Each byte indicates a run of the same value. The value of the run is
- * (byte % 5) and its length is (1 + byte / 5).
- *//* Coat of arms offsets/lengths in above RLE */
- static const short offlen[] = {
- 0, 32, 32, 32, 64, 32, 96, 32, 128, 51, 179, 32, 211, 32, 243, 92,
- 335, 37, 372, 32, 404, 32, 436, 32, 468, 58, 526, 66, 592, 56, 648,
- 56, 704, 120, 824, 38, 862, 47, 909, 62, 971, 62, 1033, 70, 1103,
- 54, 1157, 44, 1201, 42, 1243, 86, 1329, 43, 1372, 37, 1409, 36,
- 1445, 37, 1482, 37, 1519, 37, 1556, 37, 1593, 29, 1622, 52, 1674,
- 44
- };
- static unsigned long
- hash(unsigned long x)
- {
- x ^= (x & 0xffffffffUL) >> 17;
- x *= 0xed5ad4bbUL;
- x ^= (x & 0xffffffffUL) >> 11;
- x *= 0xac4c1b51UL;
- x ^= (x & 0xffffffffUL) >> 15;
- x *= 0x31848babUL;
- x ^= (x & 0xffffffffUL) >> 14;
- return x & 0xffffffffUL;
- }
- static void
- generate(unsigned char *buf, int stride, unsigned long seed)
- {
- unsigned long bits = hash(seed);
- int n = sizeof(offlen) / sizeof(*offlen) / 2;
- int s = (bits & 0xff) % n;
- bits >>= 8;
- unsigned char *ptr = buf;
- const unsigned long metals[] = {0xddd73a, 0xcac7b8};
- const unsigned long colors[] = {
- 0xff0000, 0x000000, 0x0000ff, 0x007f00,
- 0x7f007f, 0x8b004b, 0xb22222, 0xc67000
- };
- unsigned long outputs[] = {BACKGROUND, 0, 0, 0, 0};
- int bit = bits & 1;
- bits >>= 1;
- if (bit) {
- outputs[1] = metals[bits & 0x1],
- bits >>= 1;
- outputs[2] = colors[bits & 0x7],
- bits >>= 3;
- outputs[3] = metals[bits & 0x1],
- bits >>= 1;
- } else {
- outputs[1] = colors[bits & 0x7],
- bits >>= 3;
- outputs[2] = metals[bits & 0x1],
- bits >>= 1;
- outputs[3] = colors[bits & 0x7],
- bits >>= 3;
- }
- if (bits & 1) {
- outputs[4] = outputs[3];
- } else {
- outputs[4] = outputs[1];
- }
- int x = 0;
- int offset = offlen[s * 2 + 0];
- for (int i = 0; i < offlen[s * 2 + 1]; i++) {
- int in = template[offset + i];
- int len = 1 + (in / 5);
- int val = in % 5;
- unsigned long color = outputs[val];
- for (int j = 0; j < len; j++) {
- *ptr++ = color >> 16;
- *ptr++ = color >> 8;
- *ptr++ = color >> 0;
- if (++x == WIDTH) {
- x = 0;
- ptr += stride * 3;
- }
- }
- }
- }
- int
- main(void)
- {
- int width = 16;
- int height = 16;
- size_t len = PAD_WIDTH * PAD_HEIGHT * width * height * 3;
- unsigned char *buf = malloc(len);
- unsigned long seed = time(0);
- for (int y = 0; y < PAD_HEIGHT * height; y++) {
- for (int x = 0; x < PAD_WIDTH * width; x++) {
- unsigned char *p = buf + y * PAD_WIDTH * width * 3 + x * 3;
- *p++ = (unsigned char)(BACKGROUND >> 16);
- *p++ = (unsigned char)(BACKGROUND >> 8);
- *p++ = (unsigned char)(BACKGROUND >> 0);
- }
- }
- int stride = width * PAD_WIDTH - WIDTH;
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- int px = x * PAD_WIDTH + 2;
- int py = y * PAD_HEIGHT + 2;
- unsigned char *p = buf + py * width * PAD_WIDTH * 3 + px * 3;
- generate(p, stride, seed++);
- }
- }
- printf("P6\n%d %d\n255\n", PAD_WIDTH * width, PAD_HEIGHT * height);
- fwrite(buf, PAD_WIDTH * width, PAD_HEIGHT * height * 3, stdout);
- free(buf);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement