png2bng.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #define STB_IMAGE_IMPLEMENTATION
  2. #include "./stb_image.h"
  3. #include "./bng.h"
  4. int main(int argc, char **argv)
  5. {
  6. if (argc < 4) {
  7. fprintf(stderr, "png2bng <input.png> <output.bng> <pixel-format>\n");
  8. exit(1);
  9. }
  10. const char *input_filepath = argv[1];
  11. const char *output_filepath = argv[2];
  12. struct Bng_Pixel_Format desired_format = pixel_format_by_name(argv[3]);
  13. int x, y, n;
  14. uint32_t *data = (uint32_t*)stbi_load(input_filepath, &x, &y, &n, 4);
  15. assert(data != NULL);
  16. struct Bng bng = {
  17. .magic = BNG_MAGIC,
  18. .width = x,
  19. .height = y,
  20. .pixel_format = desired_format,
  21. };
  22. for (size_t i = 0; i < bng.width * bng.height; ++i) {
  23. data[i] = convert_pixel(data[i], RGBA, desired_format);
  24. }
  25. struct RLE_Pair *pairs = malloc(sizeof(struct RLE_Pair) * bng.width * bng.height);
  26. bng.pairs_count = compress_pixels(bng.width, bng.height, data, pairs);
  27. FILE *out = fopen(output_filepath, "wb");
  28. fwrite(&bng, 1, sizeof(bng), out);
  29. fwrite(pairs, 1, bng.pairs_count * sizeof(struct RLE_Pair), out);
  30. fclose(out);
  31. return 0;
  32. }