2
0

save-rgba.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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(const BitmapConstRef<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. for (int y = bitmap.height; y--;) {
  38. for (const byte *p = bitmap(0, y), *end = p+bitmap.width; p < end; ++p) {
  39. rgba[0] = rgba[1] = rgba[2] = *p;
  40. output.writePixel(rgba);
  41. }
  42. }
  43. return true;
  44. }
  45. return false;
  46. }
  47. bool saveRgba(const BitmapConstRef<byte, 3> &bitmap, const char *filename) {
  48. RgbaFileOutput output(filename, bitmap.width, bitmap.height);
  49. if (output) {
  50. byte rgba[4] = { byte(0), byte(0), byte(0), byte(0xff) };
  51. for (int y = bitmap.height; y--;) {
  52. for (const byte *p = bitmap(0, y), *end = p+3*bitmap.width; p < end; p += 3) {
  53. rgba[0] = p[0], rgba[1] = p[1], rgba[2] = p[2];
  54. output.writePixel(rgba);
  55. }
  56. }
  57. return true;
  58. }
  59. return false;
  60. }
  61. bool saveRgba(const BitmapConstRef<byte, 4> &bitmap, const char *filename) {
  62. RgbaFileOutput output(filename, bitmap.width, bitmap.height);
  63. if (output) {
  64. for (int y = bitmap.height; y--;)
  65. fwrite(bitmap(0, y), 1, 4*bitmap.width, output);
  66. return true;
  67. }
  68. return false;
  69. }
  70. bool saveRgba(const BitmapConstRef<float, 1> &bitmap, const char *filename) {
  71. RgbaFileOutput output(filename, bitmap.width, bitmap.height);
  72. if (output) {
  73. byte rgba[4] = { byte(0), byte(0), byte(0), byte(0xff) };
  74. for (int y = bitmap.height; y--;) {
  75. for (const float *p = bitmap(0, y), *end = p+bitmap.width; p < end; ++p) {
  76. rgba[0] = rgba[1] = rgba[2] = pixelFloatToByte(*p);
  77. output.writePixel(rgba);
  78. }
  79. }
  80. return true;
  81. }
  82. return false;
  83. }
  84. bool saveRgba(const BitmapConstRef<float, 3> &bitmap, const char *filename) {
  85. RgbaFileOutput output(filename, bitmap.width, bitmap.height);
  86. if (output) {
  87. byte rgba[4] = { byte(0), byte(0), byte(0), byte(0xff) };
  88. for (int y = bitmap.height; y--;) {
  89. for (const float *p = bitmap(0, y), *end = p+3*bitmap.width; p < end; p += 3) {
  90. rgba[0] = pixelFloatToByte(p[0]);
  91. rgba[1] = pixelFloatToByte(p[1]);
  92. rgba[2] = pixelFloatToByte(p[2]);
  93. output.writePixel(rgba);
  94. }
  95. }
  96. return true;
  97. }
  98. return false;
  99. }
  100. bool saveRgba(const BitmapConstRef<float, 4> &bitmap, const char *filename) {
  101. RgbaFileOutput output(filename, bitmap.width, bitmap.height);
  102. if (output) {
  103. byte rgba[4];
  104. for (int y = bitmap.height; y--;) {
  105. for (const float *p = bitmap(0, y), *end = p+4*bitmap.width; p < end; p += 4) {
  106. rgba[0] = pixelFloatToByte(p[0]);
  107. rgba[1] = pixelFloatToByte(p[1]);
  108. rgba[2] = pixelFloatToByte(p[2]);
  109. rgba[3] = pixelFloatToByte(p[3]);
  110. output.writePixel(rgba);
  111. }
  112. }
  113. return true;
  114. }
  115. return false;
  116. }
  117. }