#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; } template static void writeValueRepeated(FILE *file, T value, int times) { for (int i = 0; i < times; ++i) writeValue(file, value); } 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 > 1) writeValue(file, 0x00c2u); // Offset of 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, 0x00d2u+(channels > 1)*channels*12); // 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, 0x00c2u+(channels > 1)*channels*2); // Offset of 300, 1 // YResolution writeValue(file, 0x011bu); writeValue(file, 0x0005u); writeValue(file, 1); writeValue(file, 0x00cau+(channels > 1)*channels*2); // 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 > 1) writeValue(file, 0x00d2u+channels*2); // Offset of 3, 3, ... else { writeValue(file, 3); writeValue(file, 0); } // SMinSampleValue writeValue(file, 0x0154u); writeValue(file, 0x000bu); writeValue(file, channels); if (channels > 1) writeValue(file, 0x00d2u+channels*4); // Offset of 0.f, 0.f, ... else writeValue(file, 0.f); // SMaxSampleValue writeValue(file, 0x0155u); writeValue(file, 0x000bu); writeValue(file, channels); if (channels > 1) writeValue(file, 0x00d2u+channels*8); // Offset of 1.f, 1.f, ... else writeValue(file, 1.f); // Offset = 0x00be writeValue(file, 0); if (channels > 1) { // 0x00c2 BitsPerSample data writeValueRepeated(file, 32, channels); // 0x00c2 + 2*N XResolution data writeValue(file, 300); writeValue(file, 1); // 0x00ca + 2*N YResolution data writeValue(file, 300); writeValue(file, 1); // 0x00d2 + 2*N SampleFormat data writeValueRepeated(file, 3, channels); // 0x00d2 + 4*N SMinSampleValue data writeValueRepeated(file, 0.f, channels); // 0x00d2 + 8*N SMaxSampleValue data writeValueRepeated(file, 1.f, channels); // Offset = 0x00d2 + 12*N } else { // 0x00c2 XResolution data writeValue(file, 300); writeValue(file, 1); // 0x00ca YResolution data writeValue(file, 300); writeValue(file, 1); // Offset = 0x00d2 } return true; } template bool saveTiffFloat(const BitmapConstRef &bitmap, const char *filename) { FILE *file = fopen(filename, "wb"); if (!file) return false; writeTiffHeader(file, bitmap.width, bitmap.height, N); for (int y = bitmap.height-1; y >= 0; --y) fwrite(bitmap(0, y), sizeof(float), N*bitmap.width, file); return !fclose(file); } bool saveTiff(const BitmapConstRef &bitmap, const char *filename) { return saveTiffFloat(bitmap, filename); } bool saveTiff(const BitmapConstRef &bitmap, const char *filename) { return saveTiffFloat(bitmap, filename); } bool saveTiff(const BitmapConstRef &bitmap, const char *filename) { return saveTiffFloat(bitmap, filename); } }