save-fl32.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "save-fl32.h"
  2. #include <cstdio>
  3. namespace msdfgen {
  4. // Requires byte reversal for floats on big-endian platform
  5. #ifndef __BIG_ENDIAN__
  6. template <int N>
  7. bool saveFl32(BitmapConstSection<float, N> bitmap, const char *filename) {
  8. if (FILE *f = fopen(filename, "wb")) {
  9. bitmap.reorient(Y_UPWARD);
  10. byte header[16] = { byte('F'), byte('L'), byte('3'), byte('2') };
  11. header[4] = byte(bitmap.height);
  12. header[5] = byte(bitmap.height>>8);
  13. header[6] = byte(bitmap.height>>16);
  14. header[7] = byte(bitmap.height>>24);
  15. header[8] = byte(bitmap.width);
  16. header[9] = byte(bitmap.width>>8);
  17. header[10] = byte(bitmap.width>>16);
  18. header[11] = byte(bitmap.width>>24);
  19. header[12] = byte(N);
  20. fwrite(header, 1, 16, f);
  21. for (int y = 0; y < bitmap.height; ++y)
  22. fwrite(bitmap(0, y), sizeof(float), N*bitmap.width, f);
  23. fclose(f);
  24. return true;
  25. }
  26. return false;
  27. }
  28. template bool saveFl32(BitmapConstSection<float, 1> bitmap, const char *filename);
  29. template bool saveFl32(BitmapConstSection<float, 2> bitmap, const char *filename);
  30. template bool saveFl32(BitmapConstSection<float, 3> bitmap, const char *filename);
  31. template bool saveFl32(BitmapConstSection<float, 4> bitmap, const char *filename);
  32. #endif
  33. }