save-png.cpp 6.2 KB

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