Read TIFF format files.
139 printf(
"DEBUG readTIFF: Reading TIFF file\n");
144 char* tif_buf = NULL;
145 std::vector<TIFFDirHead> dirHead;
157 if (TIFFGetField(
tif, TIFFTAG_IMAGEWIDTH, &dhRef.
imageWidth) == 0)
159 if (TIFFGetField(
tif, TIFFTAG_IMAGELENGTH, &dhRef.
imageLength) == 0)
161 if (TIFFGetField(
tif, TIFFTAG_SUBFILETYPE, &dhRef.
subFileType) == 0)
165 TIFFGetField(
tif, TIFFTAG_RESOLUTIONUNIT, &dhRef.
resUnit);
166 TIFFGetField(
tif, TIFFTAG_XRESOLUTION, &dhRef.
xTiffRes);
167 TIFFGetField(
tif, TIFFTAG_YRESOLUTION, &dhRef.
yTiffRes);
170 if ((dhRef.
subFileType & 0x00000001) != 0x00000001)
171 dirHead.push_back(dhRef);
173 while(TIFFReadDirectory(
tif));
178 if (select_img > dirHead.size())
182 for (
size_t i = 1;
i < dirHead.size();
i++)
184 if (dirHead[0].imageLength != dirHead[
i].imageLength || \
185 dirHead[0].imageWidth != dirHead[
i].imageWidth)
193 double xRes=1.0, yRes=1.0;
195 if (dirHead[0].xTiffRes>0 && dirHead[0].yTiffRes>0)
197 switch (dirHead[0].resUnit)
206 xRes = 2.54e8/dirHead[0].xTiffRes;
207 yRes = 2.54e8/dirHead[0].yTiffRes;
210 case RESUNIT_CENTIMETER:
212 xRes = 1e8/dirHead[0].xTiffRes;
213 yRes = 1e8/dirHead[0].yTiffRes;
224 aDim.
xdim = (int) dirHead[imgStart].imageWidth;
225 aDim.
ydim = (int) dirHead[imgStart].imageLength;
230 size_t imgEnd = (select_img !=
ALL_IMAGES) ? imgStart + 1 : aDim.
ndim;
258 size_t pad = aDim.
yxdim;
262 for (
size_t i = 0;
i < aDim.
ndim;
i++)
265 uint32_t rowsperstrip;
270 unsigned int tileWidth, tileLength;
271 tileWidth = tileLength = 0;
273 for (
size_t i = imgStart;
i < imgEnd; ++
i)
275 TIFFSetDirectory(
tif,(tdir_t)
i);
280 if (dirHead[
i].samplesPerPixel > 3)
281 dirHead[
i].samplesPerPixel = 1;
283 if (TIFFIsTiled(
tif))
285 TIFFGetField(
tif, TIFFTAG_TILEWIDTH, &tileWidth);
286 TIFFGetField(
tif, TIFFTAG_TILELENGTH,&tileLength);
287 tif_buf = (
char*)_TIFFmalloc(TIFFTileSize(
tif));
291 TIFFGetFieldDefaulted(
tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
292 scanline = TIFFScanlineSize(
tif);
293 tif_buf = (
char*)_TIFFmalloc(scanline);
297 TIFFError(TIFFFileName(
tif),
"No space for strip buffer");
305 if (TIFFIsTiled(
tif))
307 for (y = 0; y < dirHead[0].imageLength; y += tileLength)
308 for (x = 0; x < dirHead[0].imageWidth; x += tileWidth)
310 TIFFReadTile(
tif, tif_buf, x, y, 0, 0);
312 swapPage((
char*)tif_buf, TIFFTileSize(
tif)*
sizeof(
unsigned char), datatype);
315 dirHead[
i].imageWidth, dirHead[
i].imageLength,
316 tileWidth, tileLength,
317 dirHead[
i].samplesPerPixel,
323 for (y = 0; y < dirHead[
i].imageLength; y++)
325 TIFFReadScanline(
tif, tif_buf, y);
327 dirHead[
i].imageWidth, dirHead[
i].imageLength,
328 dirHead[
i].samplesPerPixel,
#define REPORT_ERROR(nerr, ErrormMsg)
virtual void coreAllocateReuse()=0
unsigned short samplesPerPixel
void setValue(const MDObject &object) override
void swapPage(char *page, size_t pageNrElements, DataType datatype, int swap=1)
std::vector< std::unique_ptr< MDRow > > MD
void castTiffTile2T(size_t offset, char *tif_buf, unsigned int x, unsigned int y, unsigned int imageWidth, unsigned int imageLength, unsigned int tileWidth, unsigned int tileLength, unsigned short samplesPerPixel, DataType datatype)
void castTiffLine2T(size_t offset, char *tif_buf, unsigned int y, unsigned int imageWidth, unsigned int imageLength, unsigned short samplesPerPixel, DataType datatype)
const size_t tiff_map_min_size
virtual void setDimensions(int Xdim, int Ydim, int Zdim, size_t Ndim)=0
DataType datatypeTIFF(TIFFDirHead dHead)
unsigned short bitsPerSample
Couldn't read from file.
static MDRowVec emptyHeaderVec()
DataType datatype() const
String formatString(const char *format,...)
#define IMG_INDEX(select_img)
uint16_t imageSampleFormat
MultidimArrayBase * mdaBase
size_t gettypesize(DataType type)
Returns memory size of datatype.