save-png.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "save-png.h"
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <vector>
  6. #include "../core/pixel-conversion.hpp"
  7. #ifdef MSDFGEN_USE_LIBPNG
  8. #include <png.h>
  9. namespace msdfgen {
  10. class PngGuard {
  11. png_structp png;
  12. png_infop info;
  13. FILE *file;
  14. public:
  15. inline PngGuard(png_structp png, png_infop info) : png(png), info(info), file(NULL) { }
  16. inline ~PngGuard() {
  17. png_destroy_write_struct(&png, &info);
  18. fclose(file);
  19. }
  20. inline void setFile(FILE *file) {
  21. this->file = file;
  22. }
  23. };
  24. static void pngIgnoreError(png_structp, png_const_charp) { }
  25. static void pngWrite(png_structp png, png_bytep data, png_size_t length) {
  26. if (fwrite(data, 1, length, reinterpret_cast<FILE *>(png_get_io_ptr(png))) != length)
  27. png_error(png, "File write error");
  28. }
  29. static void pngFlush(png_structp png) {
  30. fflush(reinterpret_cast<FILE *>(png_get_io_ptr(png)));
  31. }
  32. static bool pngSave(const byte *pixels, int width, int height, int channels, int colorType, const char *filename) {
  33. if (!(pixels && width && height))
  34. return false;
  35. png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, &pngIgnoreError, &pngIgnoreError);
  36. if (!png)
  37. return false;
  38. png_infop info = png_create_info_struct(png);
  39. PngGuard guard(png, info);
  40. if (!info)
  41. return false;
  42. FILE *file = fopen(filename, "wb");
  43. if (!file)
  44. return false;
  45. guard.setFile(file);
  46. std::vector<const byte *> rows(height);
  47. for (int y = 0; y < height; ++y)
  48. rows[y] = pixels+channels*width*(height-y-1);
  49. if (setjmp(png_jmpbuf(png)))
  50. return false;
  51. png_set_write_fn(png, file, &pngWrite, &pngFlush);
  52. png_set_IHDR(png, info, width, height, 8, colorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  53. png_set_compression_level(png, 9);
  54. png_set_rows(png, info, const_cast<png_bytepp>(&rows[0]));
  55. png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
  56. return true;
  57. }
  58. static bool pngSave(const float *pixels, int width, int height, int channels, int colorType, const char *filename) {
  59. if (!(pixels && width && height))
  60. return false;
  61. int subpixels = channels*width*height;
  62. std::vector<byte> bytePixels(subpixels);
  63. for (int i = 0; i < subpixels; ++i)
  64. bytePixels[i] = pixelFloatToByte(pixels[i]);
  65. return pngSave(&bytePixels[0], width, height, channels, colorType, filename);
  66. }
  67. bool savePng(const BitmapConstRef<byte, 1> &bitmap, const char *filename) {
  68. return pngSave(bitmap.pixels, bitmap.width, bitmap.height, 1, PNG_COLOR_TYPE_GRAY, filename);
  69. }
  70. bool savePng(const BitmapConstRef<byte, 3> &bitmap, const char *filename) {
  71. return pngSave(bitmap.pixels, bitmap.width, bitmap.height, 3, PNG_COLOR_TYPE_RGB, filename);
  72. }
  73. bool savePng(const BitmapConstRef<byte, 4> &bitmap, const char *filename) {
  74. return pngSave(bitmap.pixels, bitmap.width, bitmap.height, 4, PNG_COLOR_TYPE_RGB_ALPHA, filename);
  75. }
  76. bool savePng(const BitmapConstRef<float, 1> &bitmap, const char *filename) {
  77. return pngSave(bitmap.pixels, bitmap.width, bitmap.height, 1, PNG_COLOR_TYPE_GRAY, filename);
  78. }
  79. bool savePng(const BitmapConstRef<float, 3> &bitmap, const char *filename) {
  80. return pngSave(bitmap.pixels, bitmap.width, bitmap.height, 3, PNG_COLOR_TYPE_RGB, filename);
  81. }
  82. bool savePng(const BitmapConstRef<float, 4> &bitmap, const char *filename) {
  83. return pngSave(bitmap.pixels, bitmap.width, bitmap.height, 4, PNG_COLOR_TYPE_RGB_ALPHA, filename);
  84. }
  85. }
  86. #endif
  87. #ifdef MSDFGEN_USE_LODEPNG
  88. #include <lodepng.h>
  89. namespace msdfgen {
  90. bool savePng(const BitmapConstRef<byte, 1> &bitmap, const char *filename) {
  91. std::vector<byte> pixels(bitmap.width*bitmap.height);
  92. for (int y = 0; y < bitmap.height; ++y)
  93. memcpy(&pixels[bitmap.width*y], bitmap(0, bitmap.height-y-1), bitmap.width);
  94. return !lodepng::encode(filename, pixels, bitmap.width, bitmap.height, LCT_GREY);
  95. }
  96. bool savePng(const BitmapConstRef<byte, 3> &bitmap, const char *filename) {
  97. std::vector<byte> pixels(3*bitmap.width*bitmap.height);
  98. for (int y = 0; y < bitmap.height; ++y)
  99. memcpy(&pixels[3*bitmap.width*y], bitmap(0, bitmap.height-y-1), 3*bitmap.width);
  100. return !lodepng::encode(filename, pixels, bitmap.width, bitmap.height, LCT_RGB);
  101. }
  102. bool savePng(const BitmapConstRef<byte, 4> &bitmap, const char *filename) {
  103. std::vector<byte> pixels(4*bitmap.width*bitmap.height);
  104. for (int y = 0; y < bitmap.height; ++y)
  105. memcpy(&pixels[4*bitmap.width*y], bitmap(0, bitmap.height-y-1), 4*bitmap.width);
  106. return !lodepng::encode(filename, pixels, bitmap.width, bitmap.height, LCT_RGBA);
  107. }
  108. bool savePng(const BitmapConstRef<float, 1> &bitmap, const char *filename) {
  109. std::vector<byte> pixels(bitmap.width*bitmap.height);
  110. std::vector<byte>::iterator it = pixels.begin();
  111. for (int y = bitmap.height-1; y >= 0; --y)
  112. for (int x = 0; x < bitmap.width; ++x)
  113. *it++ = pixelFloatToByte(*bitmap(x, y));
  114. return !lodepng::encode(filename, pixels, bitmap.width, bitmap.height, LCT_GREY);
  115. }
  116. bool savePng(const BitmapConstRef<float, 3> &bitmap, const char *filename) {
  117. std::vector<byte> pixels(3*bitmap.width*bitmap.height);
  118. std::vector<byte>::iterator it = pixels.begin();
  119. for (int y = bitmap.height-1; y >= 0; --y)
  120. for (int x = 0; x < bitmap.width; ++x) {
  121. *it++ = pixelFloatToByte(bitmap(x, y)[0]);
  122. *it++ = pixelFloatToByte(bitmap(x, y)[1]);
  123. *it++ = pixelFloatToByte(bitmap(x, y)[2]);
  124. }
  125. return !lodepng::encode(filename, pixels, bitmap.width, bitmap.height, LCT_RGB);
  126. }
  127. bool savePng(const BitmapConstRef<float, 4> &bitmap, const char *filename) {
  128. std::vector<byte> pixels(4*bitmap.width*bitmap.height);
  129. std::vector<byte>::iterator it = pixels.begin();
  130. for (int y = bitmap.height-1; y >= 0; --y)
  131. for (int x = 0; x < bitmap.width; ++x) {
  132. *it++ = pixelFloatToByte(bitmap(x, y)[0]);
  133. *it++ = pixelFloatToByte(bitmap(x, y)[1]);
  134. *it++ = pixelFloatToByte(bitmap(x, y)[2]);
  135. *it++ = pixelFloatToByte(bitmap(x, y)[3]);
  136. }
  137. return !lodepng::encode(filename, pixels, bitmap.width, bitmap.height, LCT_RGBA);
  138. }
  139. }
  140. #endif