save-png.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "save-png.h"
  2. #include <cstring>
  3. #include <lodepng.h>
  4. #include "../core/pixel-conversion.hpp"
  5. namespace msdfgen {
  6. bool savePng(const BitmapConstRef<byte, 1> &bitmap, const char *filename) {
  7. std::vector<byte> pixels(bitmap.width*bitmap.height);
  8. for (int y = 0; y < bitmap.height; ++y)
  9. memcpy(&pixels[bitmap.width*y], bitmap(0, bitmap.height-y-1), bitmap.width);
  10. return !lodepng::encode(filename, pixels, bitmap.width, bitmap.height, LCT_GREY);
  11. }
  12. bool savePng(const BitmapConstRef<byte, 3> &bitmap, const char *filename) {
  13. std::vector<byte> pixels(3*bitmap.width*bitmap.height);
  14. for (int y = 0; y < bitmap.height; ++y)
  15. memcpy(&pixels[3*bitmap.width*y], bitmap(0, bitmap.height-y-1), 3*bitmap.width);
  16. return !lodepng::encode(filename, pixels, bitmap.width, bitmap.height, LCT_RGB);
  17. }
  18. bool savePng(const BitmapConstRef<byte, 4> &bitmap, const char *filename) {
  19. std::vector<byte> pixels(4*bitmap.width*bitmap.height);
  20. for (int y = 0; y < bitmap.height; ++y)
  21. memcpy(&pixels[4*bitmap.width*y], bitmap(0, bitmap.height-y-1), 4*bitmap.width);
  22. return !lodepng::encode(filename, pixels, bitmap.width, bitmap.height, LCT_RGBA);
  23. }
  24. bool savePng(const BitmapConstRef<float, 1> &bitmap, const char *filename) {
  25. std::vector<byte> pixels(bitmap.width*bitmap.height);
  26. std::vector<byte>::iterator it = pixels.begin();
  27. for (int y = bitmap.height-1; y >= 0; --y)
  28. for (int x = 0; x < bitmap.width; ++x)
  29. *it++ = pixelFloatToByte(*bitmap(x, y));
  30. return !lodepng::encode(filename, pixels, bitmap.width, bitmap.height, LCT_GREY);
  31. }
  32. bool savePng(const BitmapConstRef<float, 3> &bitmap, const char *filename) {
  33. std::vector<byte> pixels(3*bitmap.width*bitmap.height);
  34. std::vector<byte>::iterator it = pixels.begin();
  35. for (int y = bitmap.height-1; y >= 0; --y)
  36. for (int x = 0; x < bitmap.width; ++x) {
  37. *it++ = pixelFloatToByte(bitmap(x, y)[0]);
  38. *it++ = pixelFloatToByte(bitmap(x, y)[1]);
  39. *it++ = pixelFloatToByte(bitmap(x, y)[2]);
  40. }
  41. return !lodepng::encode(filename, pixels, bitmap.width, bitmap.height, LCT_RGB);
  42. }
  43. bool savePng(const BitmapConstRef<float, 4> &bitmap, const char *filename) {
  44. std::vector<byte> pixels(4*bitmap.width*bitmap.height);
  45. std::vector<byte>::iterator it = pixels.begin();
  46. for (int y = bitmap.height-1; y >= 0; --y)
  47. for (int x = 0; x < bitmap.width; ++x) {
  48. *it++ = pixelFloatToByte(bitmap(x, y)[0]);
  49. *it++ = pixelFloatToByte(bitmap(x, y)[1]);
  50. *it++ = pixelFloatToByte(bitmap(x, y)[2]);
  51. *it++ = pixelFloatToByte(bitmap(x, y)[3]);
  52. }
  53. return !lodepng::encode(filename, pixels, bitmap.width, bitmap.height, LCT_RGBA);
  54. }
  55. }