save-rgba.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "save-rgba.h"
  2. #include <cstdio>
  3. #include "pixel-conversion.hpp"
  4. namespace msdfgen {
  5. class RgbaFileOutput {
  6. FILE *file;
  7. public:
  8. RgbaFileOutput(const char *filename, unsigned width, unsigned height) {
  9. if ((file = fopen(filename, "wb"))) {
  10. byte header[12] = { byte('R'), byte('G'), byte('B'), byte('A') };
  11. header[4] = byte(width>>24);
  12. header[5] = byte(width>>16);
  13. header[6] = byte(width>>8);
  14. header[7] = byte(width);
  15. header[8] = byte(height>>24);
  16. header[9] = byte(height>>16);
  17. header[10] = byte(height>>8);
  18. header[11] = byte(height);
  19. fwrite(header, 1, 12, file);
  20. }
  21. }
  22. ~RgbaFileOutput() {
  23. if (file)
  24. fclose(file);
  25. }
  26. void writePixel(const byte rgba[4]) {
  27. fwrite(rgba, 1, 4, file);
  28. }
  29. operator FILE *() {
  30. return file;
  31. }
  32. };
  33. bool saveRgba(BitmapConstSection<byte, 1> bitmap, const char *filename) {
  34. RgbaFileOutput output(filename, bitmap.width, bitmap.height);
  35. if (output) {
  36. byte rgba[4] = { byte(0), byte(0), byte(0), byte(0xff) };
  37. bitmap.reorient(Y_DOWNWARD);
  38. for (int y = 0; y < bitmap.height; ++y) {
  39. for (const byte *p = bitmap(0, y), *end = p+bitmap.width; p < end; ++p) {
  40. rgba[0] = rgba[1] = rgba[2] = *p;
  41. output.writePixel(rgba);
  42. }
  43. }
  44. return true;
  45. }
  46. return false;
  47. }
  48. bool saveRgba(BitmapConstSection<byte, 3> bitmap, const char *filename) {
  49. RgbaFileOutput output(filename, bitmap.width, bitmap.height);
  50. if (output) {
  51. byte rgba[4] = { byte(0), byte(0), byte(0), byte(0xff) };
  52. bitmap.reorient(Y_DOWNWARD);
  53. for (int y = 0; y < bitmap.height; ++y) {
  54. for (const byte *p = bitmap(0, y), *end = p+3*bitmap.width; p < end; p += 3) {
  55. rgba[0] = p[0], rgba[1] = p[1], rgba[2] = p[2];
  56. output.writePixel(rgba);
  57. }
  58. }
  59. return true;
  60. }
  61. return false;
  62. }
  63. bool saveRgba(BitmapConstSection<byte, 4> bitmap, const char *filename) {
  64. RgbaFileOutput output(filename, bitmap.width, bitmap.height);
  65. if (output) {
  66. bitmap.reorient(Y_DOWNWARD);
  67. for (int y = 0; y < bitmap.height; ++y)
  68. fwrite(bitmap(0, y), 1, 4*bitmap.width, output);
  69. return true;
  70. }
  71. return false;
  72. }
  73. bool saveRgba(BitmapConstSection<float, 1> bitmap, const char *filename) {
  74. RgbaFileOutput output(filename, bitmap.width, bitmap.height);
  75. if (output) {
  76. byte rgba[4] = { byte(0), byte(0), byte(0), byte(0xff) };
  77. bitmap.reorient(Y_DOWNWARD);
  78. for (int y = 0; y < bitmap.height; ++y) {
  79. for (const float *p = bitmap(0, y), *end = p+bitmap.width; p < end; ++p) {
  80. rgba[0] = rgba[1] = rgba[2] = pixelFloatToByte(*p);
  81. output.writePixel(rgba);
  82. }
  83. }
  84. return true;
  85. }
  86. return false;
  87. }
  88. bool saveRgba(BitmapConstSection<float, 3> bitmap, const char *filename) {
  89. RgbaFileOutput output(filename, bitmap.width, bitmap.height);
  90. if (output) {
  91. byte rgba[4] = { byte(0), byte(0), byte(0), byte(0xff) };
  92. bitmap.reorient(Y_DOWNWARD);
  93. for (int y = 0; y < bitmap.height; ++y) {
  94. for (const float *p = bitmap(0, y), *end = p+3*bitmap.width; p < end; p += 3) {
  95. rgba[0] = pixelFloatToByte(p[0]);
  96. rgba[1] = pixelFloatToByte(p[1]);
  97. rgba[2] = pixelFloatToByte(p[2]);
  98. output.writePixel(rgba);
  99. }
  100. }
  101. return true;
  102. }
  103. return false;
  104. }
  105. bool saveRgba(BitmapConstSection<float, 4> bitmap, const char *filename) {
  106. RgbaFileOutput output(filename, bitmap.width, bitmap.height);
  107. if (output) {
  108. byte rgba[4];
  109. bitmap.reorient(Y_DOWNWARD);
  110. for (int y = 0; y < bitmap.height; ++y) {
  111. for (const float *p = bitmap(0, y), *end = p+4*bitmap.width; p < end; p += 4) {
  112. rgba[0] = pixelFloatToByte(p[0]);
  113. rgba[1] = pixelFloatToByte(p[1]);
  114. rgba[2] = pixelFloatToByte(p[2]);
  115. rgba[3] = pixelFloatToByte(p[3]);
  116. output.writePixel(rgba);
  117. }
  118. }
  119. return true;
  120. }
  121. return false;
  122. }
  123. }