save-tiff.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "save-tiff.h"
  3. #include <cstdio>
  4. #ifdef MSDFGEN_USE_CPP11
  5. #include <cstdint>
  6. #else
  7. typedef int int32_t;
  8. typedef unsigned uint32_t;
  9. typedef unsigned short uint16_t;
  10. typedef unsigned char uint8_t;
  11. #endif
  12. namespace msdfgen {
  13. template <typename T>
  14. static bool writeValue(FILE *file, T value) {
  15. return fwrite(&value, sizeof(T), 1, file) == 1;
  16. }
  17. static bool writeTiffHeader(FILE *file, int width, int height, int channels) {
  18. #ifdef __BIG_ENDIAN__
  19. writeValue<uint16_t>(file, 0x4d4du);
  20. #else
  21. writeValue<uint16_t>(file, 0x4949u);
  22. #endif
  23. writeValue<uint16_t>(file, 42);
  24. writeValue<uint32_t>(file, 0x0008u); // Offset of first IFD
  25. // Offset = 0x0008
  26. writeValue<uint16_t>(file, 15); // Number of IFD entries
  27. // ImageWidth
  28. writeValue<uint16_t>(file, 0x0100u);
  29. writeValue<uint16_t>(file, 0x0004u);
  30. writeValue<uint32_t>(file, 1);
  31. writeValue<int32_t>(file, width);
  32. // ImageLength
  33. writeValue<uint16_t>(file, 0x0101u);
  34. writeValue<uint16_t>(file, 0x0004u);
  35. writeValue<uint32_t>(file, 1);
  36. writeValue<int32_t>(file, height);
  37. // BitsPerSample
  38. writeValue<uint16_t>(file, 0x0102u);
  39. writeValue<uint16_t>(file, 0x0003u);
  40. writeValue<uint32_t>(file, channels);
  41. if (channels == 3)
  42. writeValue<uint32_t>(file, 0x00c2u); // Offset of 32, 32, 32
  43. else {
  44. writeValue<uint16_t>(file, 32);
  45. writeValue<uint16_t>(file, 0);
  46. }
  47. // Compression
  48. writeValue<uint16_t>(file, 0x0103u);
  49. writeValue<uint16_t>(file, 0x0003u);
  50. writeValue<uint32_t>(file, 1);
  51. writeValue<uint16_t>(file, 1);
  52. writeValue<uint16_t>(file, 0);
  53. // PhotometricInterpretation
  54. writeValue<uint16_t>(file, 0x0106u);
  55. writeValue<uint16_t>(file, 0x0003u);
  56. writeValue<uint32_t>(file, 1);
  57. writeValue<uint16_t>(file, channels == 3 ? 2 : 1);
  58. writeValue<uint16_t>(file, 0);
  59. // StripOffsets
  60. writeValue<uint16_t>(file, 0x0111u);
  61. writeValue<uint16_t>(file, 0x0004u);
  62. writeValue<uint32_t>(file, 1);
  63. writeValue<uint32_t>(file, channels == 3 ? 0x00f6u : 0x00d2u); // Offset of pixel data
  64. // SamplesPerPixel
  65. writeValue<uint16_t>(file, 0x0115u);
  66. writeValue<uint16_t>(file, 0x0003u);
  67. writeValue<uint32_t>(file, 1);
  68. writeValue<uint16_t>(file, channels);
  69. writeValue<uint16_t>(file, 0);
  70. // RowsPerStrip
  71. writeValue<uint16_t>(file, 0x0116u);
  72. writeValue<uint16_t>(file, 0x0004u);
  73. writeValue<uint32_t>(file, 1);
  74. writeValue<int32_t>(file, height);
  75. // StripByteCounts
  76. writeValue<uint16_t>(file, 0x0117u);
  77. writeValue<uint16_t>(file, 0x0004u);
  78. writeValue<uint32_t>(file, 1);
  79. writeValue<int32_t>(file, sizeof(float)*channels*width*height);
  80. // XResolution
  81. writeValue<uint16_t>(file, 0x011au);
  82. writeValue<uint16_t>(file, 0x0005u);
  83. writeValue<uint32_t>(file, 1);
  84. writeValue<uint32_t>(file, channels == 3 ? 0x00c8u : 0x00c2u); // Offset of 300, 1
  85. // YResolution
  86. writeValue<uint16_t>(file, 0x011bu);
  87. writeValue<uint16_t>(file, 0x0005u);
  88. writeValue<uint32_t>(file, 1);
  89. writeValue<uint32_t>(file, channels == 3 ? 0x00d0u : 0x00cau); // Offset of 300, 1
  90. // ResolutionUnit
  91. writeValue<uint16_t>(file, 0x0128u);
  92. writeValue<uint16_t>(file, 0x0003u);
  93. writeValue<uint32_t>(file, 1);
  94. writeValue<uint16_t>(file, 2);
  95. writeValue<uint16_t>(file, 0);
  96. // SampleFormat
  97. writeValue<uint16_t>(file, 0x0153u);
  98. writeValue<uint16_t>(file, 0x0003u);
  99. writeValue<uint32_t>(file, channels);
  100. if (channels == 3)
  101. writeValue<uint32_t>(file, 0x00d8u); // Offset of 3, 3, 3
  102. else {
  103. writeValue<uint16_t>(file, 3);
  104. writeValue<uint16_t>(file, 0);
  105. }
  106. // SMinSampleValue
  107. writeValue<uint16_t>(file, 0x0154u);
  108. writeValue<uint16_t>(file, 0x000bu);
  109. writeValue<uint32_t>(file, channels);
  110. if (channels == 3)
  111. writeValue<uint32_t>(file, 0x00deu); // Offset of 0.f, 0.f, 0.f
  112. else
  113. writeValue<float>(file, 0.f);
  114. // SMaxSampleValue
  115. writeValue<uint16_t>(file, 0x0155u);
  116. writeValue<uint16_t>(file, 0x000bu);
  117. writeValue<uint32_t>(file, channels);
  118. if (channels == 3)
  119. writeValue<uint32_t>(file, 0x00eau); // Offset of 1.f, 1.f, 1.f
  120. else
  121. writeValue<float>(file, 1.f);
  122. // Offset = 0x00be
  123. writeValue<uint32_t>(file, 0);
  124. if (channels == 3) {
  125. // 0x00c2 BitsPerSample data
  126. writeValue<uint16_t>(file, 32);
  127. writeValue<uint16_t>(file, 32);
  128. writeValue<uint16_t>(file, 32);
  129. // 0x00c8 XResolution data
  130. writeValue<uint32_t>(file, 300);
  131. writeValue<uint32_t>(file, 1);
  132. // 0x00d0 YResolution data
  133. writeValue<uint32_t>(file, 300);
  134. writeValue<uint32_t>(file, 1);
  135. // 0x00d8 SampleFormat data
  136. writeValue<uint16_t>(file, 3);
  137. writeValue<uint16_t>(file, 3);
  138. writeValue<uint16_t>(file, 3);
  139. // 0x00de SMinSampleValue data
  140. writeValue<float>(file, 0.f);
  141. writeValue<float>(file, 0.f);
  142. writeValue<float>(file, 0.f);
  143. // 0x00ea SMaxSampleValue data
  144. writeValue<float>(file, 1.f);
  145. writeValue<float>(file, 1.f);
  146. writeValue<float>(file, 1.f);
  147. // Offset = 0x00f6
  148. } else {
  149. // 0x00c2 XResolution data
  150. writeValue<uint32_t>(file, 300);
  151. writeValue<uint32_t>(file, 1);
  152. // 0x00ca YResolution data
  153. writeValue<uint32_t>(file, 300);
  154. writeValue<uint32_t>(file, 1);
  155. // Offset = 0x00d2
  156. }
  157. return true;
  158. }
  159. bool saveTiff(const BitmapConstRef<float, 1> &bitmap, const char *filename) {
  160. FILE *file = fopen(filename, "wb");
  161. if (!file)
  162. return false;
  163. writeTiffHeader(file, bitmap.width, bitmap.height, 1);
  164. for (int y = bitmap.height-1; y >= 0; --y)
  165. fwrite(bitmap(0, y), sizeof(float), bitmap.width, file);
  166. return !fclose(file);
  167. }
  168. bool saveTiff(const BitmapConstRef<float, 3> &bitmap, const char *filename) {
  169. FILE *file = fopen(filename, "wb");
  170. if (!file)
  171. return false;
  172. writeTiffHeader(file, bitmap.width, bitmap.height, 3);
  173. for (int y = bitmap.height-1; y >= 0; --y)
  174. fwrite(bitmap(0, y), sizeof(float), 3*bitmap.width, file);
  175. return !fclose(file);
  176. }
  177. }