Advertisement
Guest User

arms.c

a guest
Dec 7th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.62 KB | None | 0 0
  1. /* https://redd.it/a3yhkf */
  2. #include <time.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define BACKGROUND 0x202020UL
  7. #define WIDTH  14
  8. #define HEIGHT 16
  9. #define PAD_WIDTH  17
  10. #define PAD_HEIGHT 20
  11.  
  12. /* Run-length encoding of coat of arms templates
  13.  *
  14.  * Each byte indicates a run of the same value. The value of the run is
  15.  * (byte % 5) and its length is (1 + byte / 5).
  16.  *//* Coat of arms offsets/lengths in above RLE */
  17. static const short offlen[] = {
  18.     0, 32, 32, 32, 64, 32, 96, 32, 128, 51, 179, 32, 211, 32, 243, 92,
  19.     335, 37, 372, 32, 404, 32, 436, 32, 468, 58, 526, 66, 592, 56, 648,
  20.     56, 704, 120, 824, 38, 862, 47, 909, 62, 971, 62, 1033, 70, 1103,
  21.     54, 1157, 44, 1201, 42, 1243, 86, 1329, 43, 1372, 37, 1409, 36,
  22.     1445, 37, 1482, 37, 1519, 37, 1556, 37, 1593, 29, 1622, 52, 1674,
  23.     44
  24. };
  25.  
  26. static unsigned long
  27. hash(unsigned long x)
  28. {
  29.     x ^= (x & 0xffffffffUL) >> 17;
  30.     x *= 0xed5ad4bbUL;
  31.     x ^= (x & 0xffffffffUL) >> 11;
  32.     x *= 0xac4c1b51UL;
  33.     x ^= (x & 0xffffffffUL) >> 15;
  34.     x *= 0x31848babUL;
  35.     x ^= (x & 0xffffffffUL) >> 14;
  36.     return x & 0xffffffffUL;
  37. }
  38.  
  39. static void
  40. generate(unsigned char *buf, int stride, unsigned long seed)
  41. {
  42.     unsigned long bits = hash(seed);
  43.     int n = sizeof(offlen) / sizeof(*offlen) / 2;
  44.     int s = (bits & 0xff) % n;
  45.     bits >>= 8;
  46.  
  47.     unsigned char *ptr = buf;
  48.  
  49.     const unsigned long metals[] = {0xddd73a, 0xcac7b8};
  50.     const unsigned long colors[] = {
  51.         0xff0000, 0x000000, 0x0000ff, 0x007f00,
  52.         0x7f007f, 0x8b004b, 0xb22222, 0xc67000
  53.     };
  54.  
  55.     unsigned long outputs[] = {BACKGROUND, 0, 0, 0, 0};
  56.     int bit = bits & 1;
  57.     bits >>= 1;
  58.     if (bit) {
  59.         outputs[1] = metals[bits & 0x1],
  60.         bits >>= 1;
  61.         outputs[2] = colors[bits & 0x7],
  62.         bits >>= 3;
  63.         outputs[3] = metals[bits & 0x1],
  64.         bits >>= 1;
  65.     } else {
  66.         outputs[1] = colors[bits & 0x7],
  67.         bits >>= 3;
  68.         outputs[2] = metals[bits & 0x1],
  69.         bits >>= 1;
  70.         outputs[3] = colors[bits & 0x7],
  71.         bits >>= 3;
  72.     }
  73.     if (bits & 1) {
  74.         outputs[4] = outputs[3];
  75.     } else {
  76.         outputs[4] = outputs[1];
  77.     }
  78.  
  79.     int x = 0;
  80.     int offset = offlen[s * 2 + 0];
  81.     for (int i = 0; i < offlen[s * 2 + 1]; i++) {
  82.         int in = template[offset + i];
  83.         int len = 1 + (in / 5);
  84.         int val = in % 5;
  85.         unsigned long color = outputs[val];
  86.         for (int j = 0; j < len; j++) {
  87.             *ptr++ = color >> 16;
  88.             *ptr++ = color >>  8;
  89.             *ptr++ = color >>  0;
  90.             if (++x == WIDTH) {
  91.                 x = 0;
  92.                 ptr += stride * 3;
  93.             }
  94.         }
  95.     }
  96. }
  97.  
  98. int
  99. main(void)
  100. {
  101.     int width = 16;
  102.     int height = 16;
  103.     size_t len = PAD_WIDTH * PAD_HEIGHT * width * height * 3;
  104.     unsigned char *buf = malloc(len);
  105.     unsigned long seed = time(0);
  106.  
  107.     for (int y = 0; y < PAD_HEIGHT * height; y++) {
  108.         for (int x = 0; x < PAD_WIDTH * width; x++) {
  109.             unsigned char *p = buf + y * PAD_WIDTH * width * 3 + x * 3;
  110.             *p++ = (unsigned char)(BACKGROUND >> 16);
  111.             *p++ = (unsigned char)(BACKGROUND >>  8);
  112.             *p++ = (unsigned char)(BACKGROUND >>  0);
  113.         }
  114.     }
  115.  
  116.     int stride = width * PAD_WIDTH - WIDTH;
  117.     for (int y = 0; y < height; y++) {
  118.         for (int x = 0; x < width; x++) {
  119.             int px = x * PAD_WIDTH + 2;
  120.             int py = y * PAD_HEIGHT + 2;
  121.             unsigned char *p = buf + py * width * PAD_WIDTH * 3 + px * 3;
  122.             generate(p, stride, seed++);
  123.         }
  124.     }
  125.  
  126.     printf("P6\n%d %d\n255\n", PAD_WIDTH * width, PAD_HEIGHT * height);
  127.     fwrite(buf, PAD_WIDTH * width, PAD_HEIGHT * height * 3, stdout);
  128.     free(buf);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement