Skip to content
Snippets Groups Projects
Commit 53941c4f authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

corr file header

parent 1faa2ae3
No related branches found
No related tags found
1 merge request!1628simplify and unify API and code for reading or writing data files
......@@ -3,7 +3,7 @@
// BornAgain: simulate and fit reflection and scattering
//
//! @file Device/IO/ReadWriteTiff.cpp
//! @brief Implements class ReadWriteTiff
//! @brief Implements functions readTiff, writeTiff
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
......@@ -26,64 +26,6 @@
#include <sstream>
#include <tiffio.hxx>
void IO::writeTiff(const Datafield& data, std::ostream& output_stream)
{
if (data.rank() != 2)
throw std::runtime_error("Cannot read TIFF file: unsupported data rank");
TIFF* tiffstream = TIFFStreamOpen("MemTIFF", &output_stream);
ASSERT(tiffstream);
const size_t m_width = data.axis(0).size();
const size_t m_height = data.axis(1).size(); // this does not exist for 1d data
//... Write header.
TIFFSetField(tiffstream, TIFFTAG_ARTIST, "BornAgain.IOFactory");
TIFFSetField(tiffstream, TIFFTAG_DATETIME, BaseUtil::System::getCurrentDateAndTime().c_str());
TIFFSetField(tiffstream, TIFFTAG_IMAGEDESCRIPTION,
"Image converted from BornAgain intensity file.");
TIFFSetField(tiffstream, TIFFTAG_SOFTWARE, "BornAgain");
const auto width = static_cast<uint32_t>(m_width);
const auto height = static_cast<uint32_t>(m_height);
TIFFSetField(tiffstream, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tiffstream, TIFFTAG_IMAGELENGTH, height);
// output format, hardcoded here
const uint16_t bitPerSample = 32;
const uint16_t samplesPerPixel = 1;
TIFFSetField(tiffstream, TIFFTAG_BITSPERSAMPLE, bitPerSample);
TIFFSetField(tiffstream, TIFFTAG_SAMPLESPERPIXEL, samplesPerPixel);
TIFFSetField(tiffstream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
//... Write data.
using sample_t = int;
const tmsize_t buf_size = sizeof(sample_t) * m_width;
const tdata_t buf = _TIFFmalloc(buf_size);
if (!buf)
throw std::runtime_error("Cannot write TIFF file: failed allocating buffer");
std::vector<sample_t> line_buf;
line_buf.resize(m_width, 0);
std::vector<unsigned> axes_indices(2);
for (unsigned row = 0; row < (uint32_t)m_height; row++) {
for (unsigned col = 0; col < line_buf.size(); ++col) {
axes_indices[0] = col;
axes_indices[1] = static_cast<unsigned>(m_height) - 1 - row;
const size_t global_index = data.frame().toGlobalIndex(axes_indices);
line_buf[col] = static_cast<sample_t>(data[global_index]);
}
memcpy(buf, &line_buf[0], buf_size);
if (TIFFWriteScanline(tiffstream, buf, row) < 0)
throw std::runtime_error("Cannot write TIFF file: error in TIFFWriteScanline");
}
_TIFFfree(buf);
TIFFFlush(tiffstream);
TIFFClose(tiffstream);
}
Datafield* IO::readTiff(std::istream& input_stream)
{
TIFF* tiffstream = TIFFStreamOpen("MemTIFF", &input_stream);
......@@ -224,4 +166,62 @@ Datafield* IO::readTiff(std::istream& input_stream)
return data.release();
}
void IO::writeTiff(const Datafield& data, std::ostream& output_stream)
{
if (data.rank() != 2)
throw std::runtime_error("Cannot read TIFF file: unsupported data rank");
TIFF* tiffstream = TIFFStreamOpen("MemTIFF", &output_stream);
ASSERT(tiffstream);
const size_t m_width = data.axis(0).size();
const size_t m_height = data.axis(1).size(); // this does not exist for 1d data
//... Write header.
TIFFSetField(tiffstream, TIFFTAG_ARTIST, "BornAgain.IOFactory");
TIFFSetField(tiffstream, TIFFTAG_DATETIME, BaseUtil::System::getCurrentDateAndTime().c_str());
TIFFSetField(tiffstream, TIFFTAG_IMAGEDESCRIPTION,
"Image converted from BornAgain intensity file.");
TIFFSetField(tiffstream, TIFFTAG_SOFTWARE, "BornAgain");
const auto width = static_cast<uint32_t>(m_width);
const auto height = static_cast<uint32_t>(m_height);
TIFFSetField(tiffstream, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tiffstream, TIFFTAG_IMAGELENGTH, height);
// output format, hardcoded here
const uint16_t bitPerSample = 32;
const uint16_t samplesPerPixel = 1;
TIFFSetField(tiffstream, TIFFTAG_BITSPERSAMPLE, bitPerSample);
TIFFSetField(tiffstream, TIFFTAG_SAMPLESPERPIXEL, samplesPerPixel);
TIFFSetField(tiffstream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
//... Write data.
using sample_t = int;
const tmsize_t buf_size = sizeof(sample_t) * m_width;
const tdata_t buf = _TIFFmalloc(buf_size);
if (!buf)
throw std::runtime_error("Cannot write TIFF file: failed allocating buffer");
std::vector<sample_t> line_buf;
line_buf.resize(m_width, 0);
std::vector<unsigned> axes_indices(2);
for (unsigned row = 0; row < (uint32_t)m_height; row++) {
for (unsigned col = 0; col < line_buf.size(); ++col) {
axes_indices[0] = col;
axes_indices[1] = static_cast<unsigned>(m_height) - 1 - row;
const size_t global_index = data.frame().toGlobalIndex(axes_indices);
line_buf[col] = static_cast<sample_t>(data[global_index]);
}
memcpy(buf, &line_buf[0], buf_size);
if (TIFFWriteScanline(tiffstream, buf, row) < 0)
throw std::runtime_error("Cannot write TIFF file: error in TIFFWriteScanline");
}
_TIFFfree(buf);
TIFFFlush(tiffstream);
TIFFClose(tiffstream);
}
#endif // BA_TIFF_SUPPORT
......@@ -3,7 +3,7 @@
// BornAgain: simulate and fit reflection and scattering
//
//! @file Device/IO/ReadWriteTiff.h
//! @brief Defines class ReadWriteTiff
//! @brief Declares functions readTiff, writeTiff
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
......@@ -25,8 +25,8 @@ class Datafield;
namespace IO {
void writeTiff(const Datafield& data, std::ostream& output_stream);
Datafield* readTiff(std::istream& input_stream);
void writeTiff(const Datafield& data, std::ostream& output_stream);
} // namespace IO
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment