Erik Fröjdh f6e76145c1
Make a library for writing and reading tiff, added tests (#347)
* removed Makefile for moench and integrated the build in CMake
* broke out tiff reading and writing to its own library
* moved tiff includes to include/sls
* moved tiffio source to src
* removed incorrectly used bps
* cleanup and tests for tiffio
* removed using namespace std from header
* some fixing for moench04
* Program for offline processing renamed

Co-authored-by: Anna Bergamaschi <anna.bergamaschi@psi.ch>
2022-01-27 10:24:02 +01:00

49 lines
1.8 KiB
C++

// SPDX-License-Identifier: LGPL-3.0-or-other
// Copyright (C) 2021 Contributors to the SLS Detector Package
#include "sls/tiffIO.h"
#include <iostream>
#include <tiffio.h>
void *WriteToTiff(float *imgData, const char *imgname, int nrow, int ncol) {
constexpr uint32_t sampleperpixel = 1;
TIFF *tif = TIFFOpen(imgname, "w");
if (tif) {
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, ncol);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, nrow);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, sampleperpixel);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,
TIFFDefaultStripSize(tif, ncol * sampleperpixel));
for (int irow = 0; irow < nrow; irow++) {
TIFFWriteScanline(tif, &imgData[irow * ncol], irow, 0);
}
TIFFClose(tif);
} else {
std::cout << "could not open file " << imgname << " for writing\n";
}
return nullptr;
}
float *ReadFromTiff(const char *imgname, uint32_t &nrow, uint32_t &ncol) {
TIFF *tif = TIFFOpen(imgname, "r");
if (tif) {
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &ncol);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &nrow);
float *imgData = new float[ncol * nrow];
for (uint32_t irow = 0; irow < nrow; ++irow) {
TIFFReadScanline(tif, &imgData[irow * ncol], irow);
}
TIFFClose(tif);
return imgData;
} else {
std::cout << "could not open file " << imgname << " for reading\n";
return nullptr;
}
}