save-png.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "save-png.h"
  2. #include <lodepng.h>
  3. #include "../core/pixel-conversion.hpp"
  4. namespace msdfgen {
  5. bool savePng(const BitmapConstRef<byte, 1> &bitmap, const char *filename) {
  6. return !lodepng::encode(filename, bitmap.pixels, bitmap.width, bitmap.height, LCT_GREY);
  7. }
  8. bool savePng(const BitmapConstRef<byte, 3> &bitmap, const char *filename) {
  9. return !lodepng::encode(filename, bitmap.pixels, bitmap.width, bitmap.height, LCT_RGB);
  10. }
  11. bool savePng(const BitmapConstRef<float, 1> &bitmap, const char *filename) {
  12. std::vector<byte> pixels(bitmap.width*bitmap.height);
  13. std::vector<byte>::iterator it = pixels.begin();
  14. for (int y = bitmap.height-1; y >= 0; --y)
  15. for (int x = 0; x < bitmap.width; ++x)
  16. *it++ = pixelFloatToByte(*bitmap(x, y));
  17. return !lodepng::encode(filename, pixels, bitmap.width, bitmap.height, LCT_GREY);
  18. }
  19. bool savePng(const BitmapConstRef<float, 3> &bitmap, const char *filename) {
  20. std::vector<byte> pixels(3*bitmap.width*bitmap.height);
  21. std::vector<byte>::iterator it = pixels.begin();
  22. for (int y = bitmap.height-1; y >= 0; --y)
  23. for (int x = 0; x < bitmap.width; ++x) {
  24. *it++ = pixelFloatToByte(bitmap(x, y)[0]);
  25. *it++ = pixelFloatToByte(bitmap(x, y)[1]);
  26. *it++ = pixelFloatToByte(bitmap(x, y)[2]);
  27. }
  28. return !lodepng::encode(filename, pixels, bitmap.width, bitmap.height, LCT_RGB);
  29. }
  30. }