save-png.cpp 1.1 KB

123456789101112131415161718192021222324252627282930
  1. #include "save-png.h"
  2. #include "../core/arithmetics.hpp"
  3. #include <lodepng.h>
  4. namespace msdfgen {
  5. bool savePng(const Bitmap<float> &bitmap, const char *filename) {
  6. std::vector<unsigned char> pixels(bitmap.width()*bitmap.height());
  7. std::vector<unsigned char>::iterator it = pixels.begin();
  8. for (int y = bitmap.height()-1; y >= 0; --y)
  9. for (int x = 0; x < bitmap.width(); ++x)
  10. *it++ = clamp(int(bitmap(x, y)*0x100), 0xff);
  11. return !lodepng::encode(filename, pixels, bitmap.width(), bitmap.height(), LCT_GREY);
  12. }
  13. bool savePng(const Bitmap<FloatRGB> &bitmap, const char *filename) {
  14. std::vector<unsigned char> pixels(3*bitmap.width()*bitmap.height());
  15. std::vector<unsigned char>::iterator it = pixels.begin();
  16. for (int y = bitmap.height()-1; y >= 0; --y)
  17. for (int x = 0; x < bitmap.width(); ++x) {
  18. *it++ = clamp(int(bitmap(x, y).r*0x100), 0xff);
  19. *it++ = clamp(int(bitmap(x, y).g*0x100), 0xff);
  20. *it++ = clamp(int(bitmap(x, y).b*0x100), 0xff);
  21. }
  22. return !lodepng::encode(filename, pixels, bitmap.width(), bitmap.height(), LCT_RGB);
  23. }
  24. }