86 auto* data =
new float[length * 2];
88 static_cast<float>(*std::max_element(data_array, data_array + length));
89 for (
unsigned j = 0; j < length; ++j)
91 data[j * 2] =
static_cast<float>(data_array[j]);
92 if (fabs(data[j * 2] - header.
no_data) <
93 std::numeric_limits<double>::epsilon())
95 data[j * 2] = max_val;
100 data[j * 2 + 1] = max_val;
104 vtkImageImport* image = vtkImageImport::New();
109 image->SetWholeExtent(0, header.
n_cols - 1, 0, header.
n_rows - 1, 0,
111 image->SetDataExtent(0, header.
n_cols - 1, 0, header.
n_rows - 1, 0,
113 image->SetDataExtentToWholeExtent();
114 image->SetDataScalarTypeToFloat();
115 image->SetNumberOfScalarComponents(2);
116 image->SetImportVoidPointer(data, 0);
125 TIFF* tiff = XTIFFOpen(fileName.c_str(),
"r");
129 GTIF* geoTiff = GTIFNew(tiff);
133 GTIFDirectoryInfo(geoTiff, version, &count);
136 WARN(
"VtkRaster::loadImageFromTIFF - file is not georeferenced.");
142 double cellsize = 1.0;
147 double* pnts =
nullptr;
153 }
while (TIFFReadDirectory(tiff));
156 "VtkRaster::loadImageFromTIFF() - File contains {:d} "
157 "images. This method is not tested for this case.",
161 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &imgWidth);
162 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &imgHeight);
167 if (TIFFGetField(tiff, GTIFF_PIXELSCALE, &pntCount, &pnts))
169 if (pnts[0] != pnts[1])
171 "VtkRaster::loadImageFromTIFF(): Original raster data "
172 "has anisotrop pixel size!");
177 if (TIFFGetField(tiff, GTIFF_TIEPOINTS, &pntCount, &pnts))
181 (imgHeight * cellsize);
186 auto* pixVal =
static_cast<uint32*
>(
187 _TIFFmalloc(imgWidth * imgHeight *
sizeof(
uint32)));
188 if ((imgWidth > 0) && (imgHeight > 0))
190 if (!TIFFReadRGBAImage(tiff, imgWidth, imgHeight, pixVal, 0))
192 ERR(
"VtkRaster::loadImageFromTIFF(): reading GeoTIFF "
203 TIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
205 uint16 *cmap_red =
nullptr, *cmap_green =
nullptr,
206 *cmap_blue =
nullptr;
207 int colormap_used = TIFFGetField(tiff, TIFFTAG_COLORMAP, &cmap_red,
208 &cmap_green, &cmap_blue);
210 auto* data =
new float[imgWidth * imgHeight * 4];
211 auto* pxl(
new int[4]);
212 for (
int j = 0; j < imgHeight; ++j)
214 int lineindex = j * imgWidth;
215 for (
int i = 0; i < imgWidth; ++i)
218 unsigned pxl_idx(lineindex + i);
219 unsigned pos = 4 * (pxl_idx);
220 if (photometric == 1 && colormap_used == 1)
222 int idx = TIFFGetR(pixVal[pxl_idx]);
223 data[pos] =
static_cast<float>(cmap_red[idx] >> 8);
225 static_cast<float>(cmap_green[idx] >> 8);
226 data[pos + 2] =
static_cast<float>(cmap_blue[idx] >> 8);
232 static_cast<float>(TIFFGetR(pixVal[pxl_idx]));
234 static_cast<float>(TIFFGetG(pixVal[pxl_idx]));
236 static_cast<float>(TIFFGetB(pixVal[pxl_idx]));
238 static_cast<float>(TIFFGetA(pixVal[pxl_idx]));
245 if (photometric == 1)
248 unsigned nPixels = 4 * imgWidth * imgHeight;
249 for (
unsigned j = 0; j < nPixels; ++j)
251 if (data[j] > max_val)
257 for (
unsigned j = 0; j < nPixels; j += 4)
259 data[j + 3] = max_val;
263 vtkImageImport* image = vtkImageImport::New();
264 image->SetDataOrigin(x0, y0, 0);
265 image->SetDataSpacing(cellsize, cellsize, cellsize);
266 image->SetWholeExtent(0, imgWidth - 1, 0, imgHeight - 1, 0, 0);
267 image->SetDataExtent(0, imgWidth - 1, 0, imgHeight - 1, 0, 0);
268 image->SetDataExtentToWholeExtent();
269 image->SetDataScalarTypeToFloat();
270 image->SetNumberOfScalarComponents(4);
271 image->SetImportVoidPointer(data, 0);
281 ERR(
"VtkRaster::loadImageFromTIFF() - File not recognised as "
286 ERR(
"VtkRaster::loadImageFromTIFF() - File not recognised as TIFF-Image.");
293 QString file_name(QString::fromStdString(fileName));
294 QFileInfo fi(file_name);
295 vtkImageReader2* image(
nullptr);
297 if (fi.suffix().toLower() ==
"png")
299 image = vtkPNGReader::New();
301 else if ((fi.suffix().toLower() ==
"tif") ||
302 (fi.suffix().toLower() ==
"tiff"))
304 image = vtkTIFFReader::New();
306 else if ((fi.suffix().toLower() ==
"jpg") ||
307 (fi.suffix().toLower() ==
"jpeg"))
309 image = vtkJPEGReader::New();
311 else if (fi.suffix().toLower() ==
"bmp")
313 image = vtkBMPReader::New();
317 ERR(
"VtkRaster::readImageFromFile(): File format not supported, please "
318 "convert to BMP, JPG, PNG or TIFF.");
322 image->SetFileName(fileName.c_str());
323 image->GetOutput()->AllocateScalars(VTK_FLOAT, 1);
351 vtkImageReader2* image)
354 if (world_file.empty())
356 WARN(
"No world file found. Image is not georeferenced.");
360 std::ifstream in(world_file.c_str());
363 ERR(
"VtkRaster::readWorldFile(): Could not open file {:s}.", filename);
369 if (!std::getline(in, line))
375 if (!(std::getline(in, line) && std::getline(in, line)))
380 if (!std::getline(in, line))
385 if (delta_x != -delta_y)
387 "Anisotropic pixel size detected ({:f} vs {:f}). An isotropic "
388 "spacing of {:f} is assumed, be aware results may be wrong.",
389 delta_x, delta_y, delta_x);
391 if (!std::getline(in, line))
397 if (!std::getline(in, line))
404 image->GetOutput()->GetDimensions(extent);
405 image->SetDataSpacing(delta_x, delta_x, delta_x);
407 double const vtk_y0 = y0 + (extent[1] * delta_y);
408 image->SetDataOrigin(x0, vtk_y0, 0);