#define _CRT_SECURE_NO_WARNINGS #include "save-tiff.h" #include #ifdef MSDFGEN_USE_CPP11 #include #else typedef int int32_t; typedef unsigned uint32_t; typedef unsigned short uint16_t; typedef unsigned char uint8_t; #endif namespace msdfgen { template static bool writeValue(FILE *file, T value) { return fwrite(&value, sizeof(T), 1, file) == 1; } static bool writeTiffHeader(FILE *file, int width, int height, int channels) { #ifdef __BIG_ENDIAN__ writeValue(file, 0x4d4du); #else writeValue(file, 0x4949u); #endif writeValue(file, 42); writeValue(file, 0x0008u); // Offset of first IFD // Offset = 0x0008 writeValue(file, 15); // Number of IFD entries // ImageWidth writeValue(file, 0x0100u); writeValue(file, 0x0004u); writeValue(file, 1); writeValue(file, width); // ImageLength writeValue(file, 0x0101u); writeValue(file, 0x0004u); writeValue(file, 1); writeValue(file, height); // BitsPerSample writeValue(file, 0x0102u); writeValue(file, 0x0003u); writeValue(file, channels); if (channels == 3) writeValue(file, 0x00c2u); // Offset of 32, 32, 32 else { writeValue(file, 32); writeValue(file, 0); } // Compression writeValue(file, 0x0103u); writeValue(file, 0x0003u); writeValue(file, 1); writeValue(file, 1); writeValue(file, 0); // PhotometricInterpretation writeValue(file, 0x0106u); writeValue(file, 0x0003u); writeValue(file, 1); writeValue(file, channels == 3 ? 2 : 1); writeValue(file, 0); // StripOffsets writeValue(file, 0x0111u); writeValue(file, 0x0004u); writeValue(file, 1); writeValue(file, channels == 3 ? 0x00f6u : 0x00d2u); // Offset of pixel data // SamplesPerPixel writeValue(file, 0x0115u); writeValue(file, 0x0003u); writeValue(file, 1); writeValue(file, channels); writeValue(file, 0); // RowsPerStrip writeValue(file, 0x0116u); writeValue(file, 0x0004u); writeValue(file, 1); writeValue(file, height); // StripByteCounts writeValue(file, 0x0117u); writeValue(file, 0x0004u); writeValue(file, 1); writeValue(file, sizeof(float)*channels*width*height); // XResolution writeValue(file, 0x011au); writeValue(file, 0x0005u); writeValue(file, 1); writeValue(file, channels == 3 ? 0x00c8u : 0x00c2u); // Offset of 300, 1 // YResolution writeValue(file, 0x011bu); writeValue(file, 0x0005u); writeValue(file, 1); writeValue(file, channels == 3 ? 0x00d0u : 0x00cau); // Offset of 300, 1 // ResolutionUnit writeValue(file, 0x0128u); writeValue(file, 0x0003u); writeValue(file, 1); writeValue(file, 2); writeValue(file, 0); // SampleFormat writeValue(file, 0x0153u); writeValue(file, 0x0003u); writeValue(file, channels); if (channels == 3) writeValue(file, 0x00d8u); // Offset of 3, 3, 3 else { writeValue(file, 3); writeValue(file, 0); } // SMinSampleValue writeValue(file, 0x0154u); writeValue(file, 0x000bu); writeValue(file, channels); if (channels == 3) writeValue(file, 0x00deu); // Offset of 0.f, 0.f, 0.f else writeValue(file, 0.f); // SMaxSampleValue writeValue(file, 0x0155u); writeValue(file, 0x000bu); writeValue(file, channels); if (channels == 3) writeValue(file, 0x00eau); // Offset of 1.f, 1.f, 1.f else writeValue(file, 1.f); // Offset = 0x00be writeValue(file, 0); if (channels == 3) { // 0x00c2 BitsPerSample data writeValue(file, 32); writeValue(file, 32); writeValue(file, 32); // 0x00c8 XResolution data writeValue(file, 300); writeValue(file, 1); // 0x00d0 YResolution data writeValue(file, 300); writeValue(file, 1); // 0x00d8 SampleFormat data writeValue(file, 3); writeValue(file, 3); writeValue(file, 3); // 0x00de SMinSampleValue data writeValue(file, 0.f); writeValue(file, 0.f); writeValue(file, 0.f); // 0x00ea SMaxSampleValue data writeValue(file, 1.f); writeValue(file, 1.f); writeValue(file, 1.f); // Offset = 0x00f6 } else { // 0x00c2 XResolution data writeValue(file, 300); writeValue(file, 1); // 0x00ca YResolution data writeValue(file, 300); writeValue(file, 1); // Offset = 0x00d2 } return true; } bool saveTiff(const BitmapConstRef &bitmap, const char *filename) { FILE *file = fopen(filename, "wb"); if (!file) return false; writeTiffHeader(file, bitmap.width, bitmap.height, 1); for (int y = bitmap.height-1; y >= 0; --y) fwrite(bitmap(0, y), sizeof(float), bitmap.width, file); return !fclose(file); } bool saveTiff(const BitmapConstRef &bitmap, const char *filename) { FILE *file = fopen(filename, "wb"); if (!file) return false; writeTiffHeader(file, bitmap.width, bitmap.height, 3); for (int y = bitmap.height-1; y >= 0; --y) fwrite(bitmap(0, y), sizeof(float), 3*bitmap.width, file); return !fclose(file); } }