41 unsigned int x,
unsigned int y,
42 unsigned int imageWidth,
unsigned int imageLength,
43 unsigned int tileWidth,
unsigned int tileLength,
44 unsigned short samplesPerPixel,
49 unsigned int x_max = x + tileWidth,
50 y_max = y + tileLength;
52 if (x_max > imageWidth)
54 if (y_max > imageLength)
58 for (j = y; j < y_max; j++)
59 for (i = x; i < x_max; i++)
60 setPage2T(offset+(j*imageWidth + i), (
char*) tif_buf+((j-y)*samplesPerPixel*typeSize*tileWidth+(i-x)*samplesPerPixel*typeSize), datatype, (
size_t) 1);
71 unsigned int imageWidth,
unsigned int imageLength,
72 unsigned short samplesPerPixel,
78 for (x = 0; x < imageWidth; x++)
79 setPage2T(offset+(y*imageWidth + x), (
char*) tif_buf+(samplesPerPixel*typeSize * x), datatype, (
size_t) 1);
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,
453 dhMain.
resUnit = RESUNIT_CENTIMETER;
470 size_t bufferSize, datasize_n;
471 bufferSize = aDim.
xdim*nBytes;
476 if ((tif_buf = (
char*)_TIFFmalloc(bufferSize)) == 0)
478 TIFFError(TIFFFileName(
tif),
"No space for strip buffer");
483 for (
size_t i = 0;
i < aDim.
ndim;
i++ )
485 TIFFSetDirectory(
tif,(tdir_t)
i + imgStart);
493 TIFFSetField(
tif, TIFFTAG_RESOLUTIONUNIT, dhMain.
resUnit);
494 TIFFSetField(
tif, TIFFTAG_XRESOLUTION, dhMain.
xTiffRes);
495 TIFFSetField(
tif, TIFFTAG_YRESOLUTION, dhMain.
yTiffRes);
496 TIFFSetField(
tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
497 TIFFSetField(
tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
499 TIFFSetField(
tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
500 TIFFSetField(
tif, TIFFTAG_SOFTWARE,
"Xmipp 3.0");
502 if (aDim.
ndim == 1 && isStack ==
false)
504 TIFFSetField(
tif, TIFFTAG_SUBFILETYPE, (
unsigned int) 0
x0);
505 TIFFSetField(
tif, TIFFTAG_PAGENUMBER, (uint16_t) 0, (uint16_t) 0);
509 TIFFSetField(
tif, TIFFTAG_SUBFILETYPE, (
unsigned int) 0x2);
510 TIFFSetField(
tif, TIFFTAG_PAGENUMBER, (uint16_t)
i, (uint16_t) aDim.
ndim);
521 for (uint32_t
y = 0;
y < aDim.
ydim;
y++)
527 (
char *)tif_buf, wDType, (
size_t) aDim.
xdim, min0, max0, castMode);
529 TIFFWriteScanline(
tif, tif_buf,
y,0);
533 TIFFWriteDirectory(
tif);
std::string datatype2Str(DataType datatype)
uint16_t imageSampleFormat
#define REPORT_ERROR(nerr, ErrormMsg)
virtual void setPage2T(size_t offset, char *page, DataType datatype, size_t pageSize)=0
virtual void coreAllocateReuse()=0
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)
unsigned short samplesPerPixel
int writeTIFF(size_t select_img, bool isStack=false, int mode=WRITE_OVERWRITE, String bitDepth="", CastWriteMode castMode=CW_CAST)
T & getValue(MDLabel label)
virtual void computeDoubleMinMaxRange(double &minval, double &maxval, size_t offset, size_t size) const =0
unsigned short bitsPerSample
const size_t tiff_map_min_size
virtual void getPageFromT(size_t offset, char *page, DataType datatype, size_t pageSize)=0
virtual void setDimensions(int Xdim, int Ydim, int Zdim, size_t Ndim)=0
DataType datatypeTIFF(TIFFDirHead dHead)
virtual bool isComplexT() const =0
Couldn't read from file.
static MDRowVec emptyHeaderVec()
int readTIFF(size_t select_img, bool isStack=false)
DataType datatype() const
virtual void getCastConvertPageFromT(size_t offset, char *page, DataType datatype, size_t pageSize, double min0, double max0, CastWriteMode castMode=CW_CONVERT) const =0
String formatString(const char *format,...)
bool empty() const override
#define IMG_INDEX(select_img)
Incorrect MultidimArray dimensions.
virtual DataType myT() const =0
MultidimArrayBase * mdaBase
DataType datatypeRAW(String strDT)
size_t gettypesize(DataType type)
Returns memory size of datatype.
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const