binviz.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <math.h>
  4. #define NOB_IMPLEMENTATION
  5. #include "nob.h"
  6. #define STB_IMAGE_WRITE_IMPLEMENTATION
  7. #include "stb_image_write.h"
  8. #define MAP_SIZE 256
  9. static size_t map[MAP_SIZE][MAP_SIZE] = {0};
  10. static uint32_t pixels[MAP_SIZE][MAP_SIZE] = {0};
  11. int main(int argc, char **argv)
  12. {
  13. const char *program = nob_shift_args(&argc, &argv);
  14. if (argc <= 0) {
  15. nob_log(NOB_ERROR, "No input is provided");
  16. nob_log(NOB_INFO, "Usage: %s <input>", program);
  17. return 1;
  18. }
  19. Nob_String_Builder content = {0};
  20. for (int i = 0; i < argc; ++i) {
  21. const char *input_file_path = argv[i];
  22. content.count = 0;
  23. nob_log(NOB_INFO, "Reading %s", input_file_path);
  24. if (!nob_read_entire_file(input_file_path, &content)) return 1;
  25. memset(map, 0, sizeof(map));
  26. if (content.count > 0) {
  27. for (size_t i = 0; i < content.count - 1; ++i) {
  28. uint8_t x = content.items[i];
  29. uint8_t y = content.items[i+1];
  30. map[y][x] += 1;
  31. }
  32. }
  33. float max = 0;
  34. for (size_t y = 0; y < MAP_SIZE; ++y) {
  35. for (size_t x = 0; x < MAP_SIZE; ++x) {
  36. float f = 0.0f;
  37. if (map[y][x] > 0) f = logf(map[y][x]);
  38. if (f > max) max = f;
  39. }
  40. }
  41. for (size_t y = 0; y < MAP_SIZE; ++y) {
  42. for (size_t x = 0; x < MAP_SIZE; ++x) {
  43. float t = logf(map[y][x])/max;
  44. uint32_t b = t*255;
  45. pixels[y][x] = 0xFF000000 | b | (b<<8) | (b<<16);
  46. }
  47. }
  48. const char *output_file_path = nob_temp_sprintf("%s.binviz.png", input_file_path);
  49. if (!stbi_write_png(output_file_path, MAP_SIZE, MAP_SIZE, 4, pixels, MAP_SIZE*sizeof(uint32_t))) {
  50. nob_log(NOB_ERROR, "Could not save image %s", output_file_path);
  51. return 1;
  52. }
  53. nob_log(NOB_INFO, "Generated %s", output_file_path);
  54. nob_temp_reset();
  55. }
  56. return 0;
  57. }