// Copyright (2019-2024) Paul Scherrer Institute #include "WriteTIFF.h" #include "../common/JFJochException.h" #ifdef JFJOCH_USE_TIFF #include #include #include #endif void WriteTIFF(TIFF *tiff, void *buff, size_t cols, size_t lines, size_t elem_size, bool is_signed) { #ifndef JFJOCH_USE_TIFF throw JFJochException(JFJochExceptionCategory::TIFFGeneratorError, "Compiled without TIFF support"); #else if (tiff == nullptr) throw JFJochException(JFJochExceptionCategory::TIFFGeneratorError, "TIFFStreamOpen error"); TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, cols); // set the width of the image TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, lines); // set the height of the image TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, 1); // set number of channels per pixel TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, elem_size * 8); // set the size of the channels TIFFSetField(tiff, TIFFTAG_COMPRESSION, COMPRESSION_LZW); // setc ompression to LZW TIFFSetField(tiff, TIFFTAG_ROWSPERSTRIP, lines); if (is_signed) TIFFSetField(tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT); else TIFFSetField(tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT); if (TIFFWriteEncodedStrip(tiff, 0, buff, cols * lines * elem_size) < 0) throw JFJochException(JFJochExceptionCategory::TIFFGeneratorError, "TIFFWriteEncodedStrip error"); #endif } std::string WriteTIFFToString(void *buff, size_t cols, size_t lines, size_t elem_size, bool is_signed) { #ifndef JFJOCH_USE_TIFF throw JFJochException(JFJochExceptionCategory::TIFFGeneratorError, "Compiled without TIFF support"); #else std::stringstream os; TIFF *tiff = TIFFStreamOpen("x", (std::ostream *) &os); WriteTIFF(tiff, buff, cols, lines, elem_size, is_signed); TIFFClose(tiff); return os.str(); #endif } void WriteTIFFToFile(const std::string &filename, void *buff, size_t cols, size_t lines, size_t elem_size, bool is_signed) { #ifndef JFJOCH_USE_TIFF throw JFJochException(JFJochExceptionCategory::TIFFGeneratorError, "Compiled without TIFF support"); #else TIFF *tiff = TIFFOpen(filename.c_str(), "w"); WriteTIFF(tiff, buff, cols, lines, elem_size, is_signed); TIFFClose(tiff); #endif }