test_bluenoise.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "par_asset.h"
  2. #include "lodepng.h"
  3. #define PAR_BLUENOISE_IMPLEMENTATION
  4. #include "par_bluenoise.h"
  5. const int MAXPTS = 100000;
  6. const int DENSITY = 200000;
  7. const int RESOLUTION = 384;
  8. #define CLAMP(x, min, max) ((x < min) ? min : ((x > max) ? max : x))
  9. static void test_bluenoise()
  10. {
  11. int nbytes;
  12. par_byte* data;
  13. // Download and decode the density image.
  14. asset_get("trillium.png", &data, &nbytes);
  15. unsigned dims[2] = {0, 0};
  16. unsigned char* pixels;
  17. lodepng_decode_memory(&pixels, &dims[0], &dims[1], data, nbytes, LCT_GREY,
  18. 8);
  19. free(data);
  20. // Download the tileset and initialize the bluenoise context.
  21. nbytes = 0;
  22. asset_get("bluenoise.trimmed.bin", &data, &nbytes);
  23. par_bluenoise_context* ctx;
  24. ctx = par_bluenoise_from_buffer(data, nbytes, MAXPTS);
  25. free(data);
  26. // Copy the density image into the bluenoise context and free it.
  27. par_bluenoise_density_from_gray(ctx, pixels, dims[0], dims[1], 1);
  28. free(pixels);
  29. // Generate points.
  30. float left = -0.5;
  31. float bottom = -0.5;
  32. float right = 0.5;
  33. float top = 0.5;
  34. int npts;
  35. par_bluenoise_set_viewport(ctx, left, bottom, right, top);
  36. float* cpupts = par_bluenoise_generate(ctx, DENSITY, &npts);
  37. par_bluenoise_free(ctx);
  38. // Draw points.
  39. pixels = malloc(RESOLUTION * RESOLUTION);
  40. memset(pixels, 0xff, RESOLUTION * RESOLUTION);
  41. for (int i = 0; i < npts; i++) {
  42. float x = *cpupts++;
  43. float y = *cpupts++;
  44. cpupts++;
  45. int i = CLAMP(RESOLUTION * (x + 0.5), 0, RESOLUTION - 1);
  46. int j = CLAMP(RESOLUTION * (0.5 - y), 0, RESOLUTION - 1);
  47. pixels[i + j * RESOLUTION] = 0;
  48. }
  49. // Write out the image.
  50. lodepng_encode_file("build/bluenoise.png", pixels, RESOLUTION, RESOLUTION,
  51. LCT_GREY, 8);
  52. free(pixels);
  53. }
  54. int main(int argc, char* argv[])
  55. {
  56. asset_init();
  57. test_bluenoise();
  58. return 0;
  59. }