summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 30608312b4e884fdea473e25537c0bdc4053a187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*********************************************************************
 * kd-forest                                                         *
 * Copyright (C) 2014 Tavian Barnes <tavianator@tavianator.com>      *
 *                                                                   *
 * This program is free software. It comes without any warranty, to  *
 * the extent permitted by applicable law. You can redistribute it   *
 * and/or modify it under the terms of the Do What The Fuck You Want *
 * To Public License, Version 2, as published by Sam Hocevar. See    *
 * the COPYING file or http://www.wtfpl.net/ for more details.       *
 *********************************************************************/

#include "kd-forest.h"
#include "util.h"
#include "color.h"
#include <errno.h>
#include <math.h>
#include <png.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#if __unix__
#  include <unistd.h>
#endif

// Number of trailing zero bits on each color chanel, set to zero for all
// 24-bit images
#define BIT_DEPTH 24
// Whether to sort by hue
#define HUE_SORT true

// Which color space to use
#define USE_RGB false
#define USE_LAB true
#define USE_LUV false

// Computed constants
static const unsigned int WIDTH = 1U << (BIT_DEPTH + 1)/2; // Round up
static const unsigned int HEIGHT = 1U << (BIT_DEPTH)/2;    // Round down
static const unsigned int SIZE = 1U << BIT_DEPTH;

static unsigned int
rand_in(unsigned int range)
{
  // Compensate for bias if (RAND_MAX + 1) isn't a multiple of range
  unsigned int limit = RAND_MAX + 1U - ((RAND_MAX + 1U)%range);
  unsigned int res;
  do {
    res = rand();
  } while (res >= limit);
  return res%range;
}

static kd_node_t *
try_neighbor(kd_node_t *node, int dx, int dy)
{
  if (dx < 0 && node->x < -dx) {
    return NULL;
  } else if (dx > 0 && node->x + dx >= WIDTH) {
    return NULL;
  } else if (dy < 0 && node->y < -dy) {
    return NULL;
  } else if (dy > 0 && node->y + dy >= HEIGHT) {
    return NULL;
  }

  return node + (int)WIDTH*dy + dx;
}

// Star pattern
static int neighbor_order[][2] = {
  { -1, -1 },
  {  0, +1 },
  { +1, -1 },
  { -1,  0 },
  { +1, +1 },
  {  0, -1 },
  { -1, +1 },
  { +1,  0 },
};

static kd_node_t *
next_neighbor(kd_node_t *node)
{
  unsigned int first = rand_in(8);
  for (unsigned int i = first; i < first + 8; ++i) {
    int *delta = neighbor_order[i%8];
    kd_node_t *neighbor = try_neighbor(node, delta[0], delta[1]);
    if (neighbor && !neighbor->added) {
      return neighbor;
    }
  }

  return NULL;
}

static void
remove_if_surrounded(kd_forest_t *kdf, kd_node_t *node)
{
  if (node->added && !node->removed && next_neighbor(node) == NULL) {
    kdf_remove(kdf, node);
  }
}

static void
remove_non_boundary(kd_forest_t *kdf, kd_node_t *node)
{
  for (int dy = -1; dy <= 1; ++dy) {
    for (int dx = -1; dx <= 1; ++dx) {
      kd_node_t *neighbor = try_neighbor(node, dx, dy);
      if (neighbor) {
        remove_if_surrounded(kdf, neighbor);
      }
    }
  }
}

static uint32_t *
create_colors(void)
{
  // From least to most perceptually important
  const unsigned int bskip = 1U << (24 - BIT_DEPTH)/3;
  const unsigned int rskip = 1U << (24 - BIT_DEPTH + 1)/3;
  const unsigned int gskip = 1U << (24 - BIT_DEPTH + 2)/3;

  uint32_t *colors = xmalloc(SIZE*sizeof(uint32_t));
  for (unsigned int b = 0, i = 0; b < 0x100; b += bskip) {
    for (unsigned int g = 0; g < 0x100; g += gskip) {
      for (unsigned int r = 0; r < 0x100; r += rskip, ++i) {
        colors[i] = (r << 16) | (g << 8) | b;
      }
    }
  }

  if (HUE_SORT) {
    qsort(colors, SIZE, sizeof(uint32_t), color_comparator);
  } else {
    // Fisher-Yates shuffle
    for (unsigned int i = SIZE; i-- > 0;) {
      unsigned int j = rand_in(i + 1);
      uint32_t temp = colors[i];
      colors[i] = colors[j];
      colors[j] = temp;
    }
  }

  return colors;
}

static kd_node_t *
create_kd_nodes(void)
{
  return xmalloc(SIZE*sizeof(kd_node_t));
}

static png_byte **
create_bitmap(void)
{
  png_byte **rows = xmalloc(HEIGHT*sizeof(png_byte *));
  const size_t row_size = 3*WIDTH*sizeof(png_byte);
  for (unsigned int i = 0; i < HEIGHT; ++i) {
    rows[i] = xmalloc(row_size);
    memset(rows[i], 0, row_size);
  }
  return rows;
}

static void
generate_image(const uint32_t *colors, kd_node_t *nodes,
               unsigned int initial_x, unsigned int initial_y,
               png_byte **bitmap)
{
  for (unsigned int y = 0, i = 0; y < HEIGHT; ++y) {
    for (unsigned int x = 0; x < WIDTH; ++x, ++i) {
      kd_node_init(nodes + y*WIDTH + x, x, y);
    }
  }

  // Make the forest
  kd_forest_t kdf;
  kdf_init(&kdf);

#if __unix__
  bool tty = isatty(1);
  const char *clear_line = tty ? "\033[2K\r" : "";
  const char *new_line = tty ? "" : "\n";
#else
  const char *clear_line = "";
  const char *new_line = "\n";
#endif

  size_t max_size = 0;

  // Do multiple passes to get rid of artifacts in HUE_SORT mode
  for (unsigned int i = 1, progress = 0; i <= BIT_DEPTH; ++i) {
    unsigned int stripe = 1 << i;

    for (unsigned int j = stripe/2 - 1; j < SIZE; j += stripe, ++progress) {
      if (progress%WIDTH == 0) {
        printf("%s%.2f%%\t| boundary size: %zu\t| max boundary size: %zu%s",
               clear_line, 100.0*progress/SIZE, kdf.size, max_size, new_line);
        fflush(stdout);
      }

      uint32_t color = colors[j];

      kd_node_t target;
#if USE_RGB
      color_set_RGB(target.coords, color);
#elif USE_LAB
      color_set_Lab(target.coords, color);
#elif USE_LUV
      color_set_Luv(target.coords, color);
#else
#  error "Pick one!"
#endif

      kd_node_t *new_node;
      if (j == 0) {
        // First node goes in the center
        new_node = nodes + WIDTH*initial_y + initial_x;
      } else {
        kd_node_t *nearest = kdf_find_nearest(&kdf, &target);
        new_node = next_neighbor(nearest);
      }

      memcpy(new_node->coords, target.coords, sizeof(target.coords));
      kdf_insert(&kdf, new_node);
      remove_non_boundary(&kdf, new_node);

      if (kdf.size > max_size) {
        max_size = kdf.size;
      }

      png_byte *pixel = bitmap[new_node->y] + 3*new_node->x;
      color_unpack(pixel, color);
    }
  }

  printf("%s%.2f%%\t| boundary size: 0\t| max boundary size: %zu\n",
         clear_line, 100.0, max_size);

  kdf_destroy(&kdf);
}

static void
write_png(const char *filename, png_byte **bitmap)
{
  FILE *file = fopen(filename, "wb");
  if (!file) {
    abort();
  }

  png_struct *png_ptr =
    png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if (!png_ptr) {
    abort();
  }

  png_info *info_ptr = png_create_info_struct(png_ptr);
  if (!info_ptr) {
    abort();
  }

  // libpng will longjmp here if it encounters an error from now on
  if (setjmp(png_jmpbuf(png_ptr))) {
    abort();
  }

  png_init_io(png_ptr, file);
  png_set_IHDR(png_ptr, info_ptr, WIDTH, HEIGHT, 8,
               PNG_COLOR_TYPE_RGB, PNG_INTERLACE_ADAM7,
               PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, PNG_sRGB_INTENT_ABSOLUTE);
  png_set_rows(png_ptr, info_ptr, bitmap);
  png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
  png_destroy_write_struct(&png_ptr, &info_ptr);
  fclose(file);
}

int
main(void)
{
  printf("Generating a %ux%u image (%u pixels)\n", WIDTH, HEIGHT, SIZE);

  // For consistent images
  srand(0);

  // Generate all the colors
  uint32_t *colors = create_colors();
  // Make a pool of potential k-d nodes
  kd_node_t *nodes = create_kd_nodes();

  // Allocate the bitmap
  png_byte **bitmap = create_bitmap();

  // Generate the image
  generate_image(colors, nodes, WIDTH/2, HEIGHT/2, bitmap);

  // Write out the image
  write_png("kd-forest.png", bitmap);

  // Clean up
  for (unsigned int i = 0; i < HEIGHT; ++i) {
    free(bitmap[i]);
  }
  free(bitmap);
  free(nodes);
  free(colors);
  return 0;
}