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

[j.833] correct Tiff r/w, fix Galaxi example read-in (#833) (Closes #833)

Merging branch 'j.833'  into 'main'.

See merge request !2132
parents 1588714c 26fe8d1a
No related branches found
No related tags found
1 merge request!2132correct Tiff r/w, fix Galaxi example read-in (#833)
Pipeline #119977 passed
......@@ -102,13 +102,10 @@ Datafield* Util::RW::readTiff(std::istream& input_stream)
//... Read data.
const size_t width = (size_t)w;
const size_t height = (size_t)h;
ASSERT(0 == bitsPerSample % 8);
uint16_t bytesPerSample = bitsPerSample / 8;
tmsize_t buf_size = TIFFScanlineSize(tiffstream);
tmsize_t expected_size = bytesPerSample * width;
tmsize_t expected_size = bytesPerSample * w;
if (buf_size != expected_size)
throw std::runtime_error("Cannot read TIFF file: wrong scanline size" + ref_to_doc);
......@@ -117,25 +114,19 @@ Datafield* Util::RW::readTiff(std::istream& input_stream)
throw std::runtime_error("Cannot read TIFF file: failed allocating buffer" + ref_to_doc);
auto data = std::make_unique<Datafield>(
std::vector<const Scale*>{newEquiDivision("u (bin)", width, 0.0, double(width)),
newEquiDivision("v (bin)", height, 0.0, double(height))});
std::vector<const Scale*>{newEquiDivision("u (bin)", w, 0.0, double(w)),
newEquiDivision("v (bin)", h, 0.0, double(h))});
std::vector<int8_t> line_buf;
line_buf.resize(buf_size, 0);
std::vector<unsigned> axes_indices(2);
for (uint32_t row = 0; row < (uint32_t)height; row++) {
for (uint32_t row = 0; row < h; row++) {
if (TIFFReadScanline(tiffstream, buf, row) < 0)
throw std::runtime_error("Cannot read TIFF file: error in scanline." + ref_to_doc);
memcpy(&line_buf[0], buf, buf_size);
for (unsigned col = 0; col < width; ++col) {
axes_indices[0] = col;
axes_indices[1] = static_cast<unsigned>(height) - 1 - row;
size_t global_index = data->frame().toGlobalIndex(axes_indices);
for (unsigned col = 0; col < w; ++col) {
void* incoming = &line_buf[col * bytesPerSample];
double sample = 0;
......@@ -174,7 +165,7 @@ Datafield* Util::RW::readTiff(std::istream& input_stream)
+ ref_to_doc);
}
(*data)[global_index] = sample;
(*data)[(h - 1 - row) * w + col] = sample;
}
}
_TIFFfree(buf);
......@@ -223,14 +214,9 @@ void Util::RW::writeTiff(const Datafield& data, std::ostream& output_stream)
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]);
}
for (unsigned col = 0; col < line_buf.size(); ++col)
line_buf[col] = static_cast<sample_t>(data[(m_height - 1 - row) * m_width + col]);
memcpy(buf, &line_buf[0], buf_size);
if (TIFFWriteScanline(tiffstream, buf, row) < 0)
......
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