diff --git a/Device/IO/ReadWriteTiff.cpp b/Device/IO/ReadWriteTiff.cpp index 14f75f6f78b72e8dedee659a5919074fac9d71a7..bfc9c2ccd18a240a5fa81a55d352d5cb7424b982 100644 --- a/Device/IO/ReadWriteTiff.cpp +++ b/Device/IO/ReadWriteTiff.cpp @@ -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 diff --git a/Device/IO/ReadWriteTiff.h b/Device/IO/ReadWriteTiff.h index 059ee62148d83d42ba7a005c1329d8a086975ac7..847a2d6fcaa7e3b9288a84b6484d47664414a574 100644 --- a/Device/IO/ReadWriteTiff.h +++ b/Device/IO/ReadWriteTiff.h @@ -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