75 auto* data =
new float[length * 2];
77 static_cast<float>(*std::max_element(data_array, data_array + length));
78 for (
unsigned j = 0; j < length; ++j)
80 data[j * 2] =
static_cast<float>(data_array[j]);
81 if (fabs(data[j * 2] - header.
no_data) <
82 std::numeric_limits<double>::epsilon())
84 data[j * 2] = max_val;
89 data[j * 2 + 1] = max_val;
93 vtkImageImport* image = vtkImageImport::New();
98 image->SetWholeExtent(0, header.
n_cols - 1, 0, header.
n_rows - 1, 0,
100 image->SetDataExtent(0, header.
n_cols - 1, 0, header.
n_rows - 1, 0,
102 image->SetDataExtentToWholeExtent();
103 image->SetDataScalarTypeToFloat();
104 image->SetNumberOfScalarComponents(2);
105 image->SetImportVoidPointer(data, 0);
114 TIFF* tiff = XTIFFOpen(fileName.c_str(),
"r");
118 GTIF* geoTiff = GTIFNew(tiff);
122 GTIFDirectoryInfo(geoTiff, version, &count);
125 WARN(
"VtkRaster::loadImageFromTIFF - file is not georeferenced.");
131 double cellsize = 1.0;
136 double* pnts =
nullptr;
142 }
while (TIFFReadDirectory(tiff));
145 "VtkRaster::loadImageFromTIFF() - File contains {:d} "
146 "images. This method is not tested for this case.",
150 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &imgWidth);
151 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &imgHeight);
156 if (TIFFGetField(tiff, GTIFF_PIXELSCALE, &pntCount, &pnts))
158 if (pnts[0] != pnts[1])
160 "VtkRaster::loadImageFromTIFF(): Original raster data "
161 "has anisotrop pixel size!");
166 if (TIFFGetField(tiff, GTIFF_TIEPOINTS, &pntCount, &pnts))
170 (imgHeight * cellsize);
175 auto* pixVal =
static_cast<uint32*
>(
176 _TIFFmalloc(imgWidth * imgHeight *
sizeof(
uint32)));
177 if ((imgWidth > 0) && (imgHeight > 0))
179 if (!TIFFReadRGBAImage(tiff, imgWidth, imgHeight, pixVal, 0))
181 ERR(
"VtkRaster::loadImageFromTIFF(): reading GeoTIFF "
192 TIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
194 uint16 *cmap_red =
nullptr, *cmap_green =
nullptr,
195 *cmap_blue =
nullptr;
196 int colormap_used = TIFFGetField(tiff, TIFFTAG_COLORMAP, &cmap_red,
197 &cmap_green, &cmap_blue);
199 auto* data =
new float[imgWidth * imgHeight * 4];
200 auto* pxl(
new int[4]);
201 for (
int j = 0; j < imgHeight; ++j)
203 int lineindex = j * imgWidth;
204 for (
int i = 0; i < imgWidth; ++i)
207 unsigned pxl_idx(lineindex + i);
208 unsigned pos = 4 * (pxl_idx);
209 if (photometric == 1 && colormap_used == 1)
211 int idx = TIFFGetR(pixVal[pxl_idx]);
212 data[pos] =
static_cast<float>(cmap_red[idx] >> 8);
214 static_cast<float>(cmap_green[idx] >> 8);
215 data[pos + 2] =
static_cast<float>(cmap_blue[idx] >> 8);
221 static_cast<float>(TIFFGetR(pixVal[pxl_idx]));
223 static_cast<float>(TIFFGetG(pixVal[pxl_idx]));
225 static_cast<float>(TIFFGetB(pixVal[pxl_idx]));
227 static_cast<float>(TIFFGetA(pixVal[pxl_idx]));
234 if (photometric == 1)
237 unsigned nPixels = 4 * imgWidth * imgHeight;
238 for (
unsigned j = 0; j < nPixels; ++j)
240 if (data[j] > max_val)
246 for (
unsigned j = 0; j < nPixels; j += 4)
248 data[j + 3] = max_val;
252 vtkImageImport* image = vtkImageImport::New();
253 image->SetDataOrigin(x0, y0, 0);
254 image->SetDataSpacing(cellsize, cellsize, cellsize);
255 image->SetWholeExtent(0, imgWidth - 1, 0, imgHeight - 1, 0, 0);
256 image->SetDataExtent(0, imgWidth - 1, 0, imgHeight - 1, 0, 0);
257 image->SetDataExtentToWholeExtent();
258 image->SetDataScalarTypeToFloat();
259 image->SetNumberOfScalarComponents(4);
260 image->SetImportVoidPointer(data, 0);
270 ERR(
"VtkRaster::loadImageFromTIFF() - File not recognised as "
275 ERR(
"VtkRaster::loadImageFromTIFF() - File not recognised as TIFF-Image.");
282 QString file_name(QString::fromStdString(fileName));
283 QFileInfo fi(file_name);
284 vtkImageReader2* image(
nullptr);
286 if (fi.suffix().toLower() ==
"png")
288 image = vtkPNGReader::New();
290 else if ((fi.suffix().toLower() ==
"tif") ||
291 (fi.suffix().toLower() ==
"tiff"))
293 image = vtkTIFFReader::New();
295 else if ((fi.suffix().toLower() ==
"jpg") ||
296 (fi.suffix().toLower() ==
"jpeg"))
298 image = vtkJPEGReader::New();
300 else if (fi.suffix().toLower() ==
"bmp")
302 image = vtkBMPReader::New();
306 ERR(
"VtkRaster::readImageFromFile(): File format not supported, please "
307 "convert to BMP, JPG, PNG or TIFF.");
311 image->SetFileName(fileName.c_str());
312 image->GetOutput()->AllocateScalars(VTK_FLOAT, 1);
340 vtkImageReader2* image)
343 if (world_file.empty())
345 WARN(
"No world file found. Image is not georeferenced.");
349 std::ifstream in(world_file.c_str());
352 ERR(
"VtkRaster::readWorldFile(): Could not open file {:s}.", filename);
358 if (!std::getline(in, line))
364 if (!(std::getline(in, line) && std::getline(in, line)))
369 if (!std::getline(in, line))
374 if (delta_x != -delta_y)
376 "Anisotropic pixel size detected ({:f} vs {:f}). An isotropic "
377 "spacing of {:f} is assumed, be aware results may be wrong.",
378 delta_x, delta_y, delta_x);
380 if (!std::getline(in, line))
386 if (!std::getline(in, line))
393 image->GetOutput()->GetDimensions(extent);
394 image->SetDataSpacing(delta_x, delta_x, delta_x);
396 double const vtk_y0 = y0 + (extent[1] * delta_y);
397 image->SetDataOrigin(x0, vtk_y0, 0);