diff --git a/Device/IO/ReadWriteTiff.cpp b/Device/IO/ReadWriteTiff.cpp index 910284c2d9d8f33e9efc9ecec7eead528857fc02..0190a1caab5f17419dc269d16bc499942c9a1a5c 100644 --- a/Device/IO/ReadWriteTiff.cpp +++ b/Device/IO/ReadWriteTiff.cpp @@ -86,19 +86,17 @@ void IO::writeTiff(const Datafield& data, std::ostream& output_stream) Datafield* IO::readTiff(std::istream& input_stream) { - TIFF* m_tiff; uint16_t m_bitsPerSample, m_samplesPerPixel, m_sampleFormat; - std::unique_ptr<Datafield> m_data; - m_tiff = TIFFStreamOpen("MemTIFF", &input_stream); - if (!m_tiff) + TIFF* tiffstream = TIFFStreamOpen("MemTIFF", &input_stream); + if (!tiffstream) throw std::runtime_error("Cannot open the TIFF file"); //... Read header. uint32_t w, h; - if (!TIFFGetField(m_tiff, TIFFTAG_IMAGEWIDTH, &w) - || !TIFFGetField(m_tiff, TIFFTAG_IMAGELENGTH, &h)) { + if (!TIFFGetField(tiffstream, TIFFTAG_IMAGEWIDTH, &w) + || !TIFFGetField(tiffstream, TIFFTAG_IMAGELENGTH, &h)) { throw std::runtime_error("Cannot read TIFF file: missing width/height in header"); } @@ -106,23 +104,23 @@ Datafield* IO::readTiff(std::istream& input_stream) const size_t height = (size_t)h; uint16_t orientationTag(0); - TIFFGetField(m_tiff, TIFFTAG_ORIENTATION, &orientationTag); + TIFFGetField(tiffstream, TIFFTAG_ORIENTATION, &orientationTag); bool good = true; // BitsPerSample defaults to 1 according to the TIFF spec. - if (!TIFFGetField(m_tiff, TIFFTAG_BITSPERSAMPLE, &m_bitsPerSample)) + if (!TIFFGetField(tiffstream, TIFFTAG_BITSPERSAMPLE, &m_bitsPerSample)) m_bitsPerSample = 1; if (8 != m_bitsPerSample && 16 != m_bitsPerSample && 32 != m_bitsPerSample) good = false; // they may be e.g. grayscale with 2 samples per pixel - if (!TIFFGetField(m_tiff, TIFFTAG_SAMPLESPERPIXEL, &m_samplesPerPixel)) + if (!TIFFGetField(tiffstream, TIFFTAG_SAMPLESPERPIXEL, &m_samplesPerPixel)) m_samplesPerPixel = 1; if (m_samplesPerPixel != 1) good = false; - if (!TIFFGetField(m_tiff, TIFFTAG_SAMPLEFORMAT, &m_sampleFormat)) + if (!TIFFGetField(tiffstream, TIFFTAG_SAMPLEFORMAT, &m_sampleFormat)) m_sampleFormat = 1; switch (m_sampleFormat) { @@ -150,7 +148,7 @@ Datafield* IO::readTiff(std::istream& input_stream) ASSERT(0 == m_bitsPerSample % 8); uint16_t bytesPerSample = m_bitsPerSample / 8; - tmsize_t buf_size = TIFFScanlineSize(m_tiff); + tmsize_t buf_size = TIFFScanlineSize(tiffstream); tmsize_t expected_size = bytesPerSample * width; if (buf_size != expected_size) throw std::runtime_error("Cannot read TIFF file: wrong scanline size"); @@ -159,8 +157,9 @@ Datafield* IO::readTiff(std::istream& input_stream) if (!buf) throw std::runtime_error("Cannot read TIFF file: failed allocating buffer"); - m_data.reset(new Datafield({newEquiDivision("x", width, 0.0, double(width)), - newEquiDivision("y", height, 0.0, double(height))})); + auto data = std::make_unique<Datafield>( + std::vector<const Scale*>{newEquiDivision("x", width, 0.0, double(width)), + newEquiDivision("y", height, 0.0, double(height))}); std::vector<int8_t> line_buf; line_buf.resize(buf_size, 0); @@ -168,7 +167,7 @@ Datafield* IO::readTiff(std::istream& input_stream) std::vector<unsigned> axes_indices(2); for (uint32_t row = 0; row < (uint32_t)height; row++) { - if (TIFFReadScanline(m_tiff, buf, row) < 0) + if (TIFFReadScanline(tiffstream, buf, row) < 0) throw std::runtime_error("Cannot read TIFF file: error in scanline."); memcpy(&line_buf[0], buf, buf_size); @@ -176,7 +175,7 @@ Datafield* IO::readTiff(std::istream& input_stream) for (unsigned col = 0; col < width; ++col) { axes_indices[0] = col; axes_indices[1] = static_cast<unsigned>(height) - 1 - row; - size_t global_index = m_data->frame().toGlobalIndex(axes_indices); + size_t global_index = data->frame().toGlobalIndex(axes_indices); void* incoming = &line_buf[col * bytesPerSample]; double sample = 0; @@ -215,13 +214,13 @@ Datafield* IO::readTiff(std::istream& input_stream) throw std::runtime_error("Cannot read TIFF file: unexpected sample format"); } - (*m_data)[global_index] = sample; + (*data)[global_index] = sample; } } _TIFFfree(buf); - TIFFClose(m_tiff); + TIFFClose(tiffstream); - return m_data->clone(); + return data.release(); } #endif // BA_TIFF_SUPPORT