-
Notifications
You must be signed in to change notification settings - Fork 17
/
voxelTiff.h
334 lines (242 loc) · 10.4 KB
/
voxelTiff.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#ifndef VOXELTIFF_H
#define VOXELTIFF_H
#include <stdio.h>
#include <stdlib.h>
#include <typeinfo>
#include <typeindex>
#include <unordered_map>
#include <string.h>
#include <iostream>
#include "tiffio.h"
#include "voxelImage.h"
//#define streq(a,b) (strcmp(a,b) == 0)
//#define strneq(a,b,n) (strncmp(a,b,n) == 0)
//https://stackoverflow.com/questions/24059421/adding-custom-tags-to-a-tiff-file, didn't work properly, also a too dirty solution
template<typename T>
int tifDataType(T)
{
const std::unordered_map<std::type_index, int> tifTypes
{
{std::type_index(typeid(unsigned char)), SAMPLEFORMAT_UINT},
{std::type_index(typeid(char)), SAMPLEFORMAT_INT},
{std::type_index(typeid(int)), SAMPLEFORMAT_INT},
{std::type_index(typeid(unsigned int)), SAMPLEFORMAT_UINT},
{std::type_index(typeid(short)), SAMPLEFORMAT_INT},
{std::type_index(typeid(unsigned short)), SAMPLEFORMAT_UINT},
{std::type_index(typeid(float)), SAMPLEFORMAT_IEEEFP},
{std::type_index(typeid(double)), SAMPLEFORMAT_IEEEFP},
};
return tifTypes.at(std::type_index(typeid(T)));
}
inline void getTifTags(dbl3& X0_, dbl3& dx_, TIFF *tif)
{
float dx,dy;
TIFFGetField(tif, TIFFTAG_XRESOLUTION, &dx);
TIFFGetField(tif, TIFFTAG_YRESOLUTION, &dy);
dx_={dx,dy,dy};
float X0=0., Y0=0., Z0=-2.1e30;
TIFFGetField(tif, TIFFTAG_XPOSITION, &X0);
TIFFGetField(tif, TIFFTAG_YPOSITION, &Y0);
uint32 kFrst=0;
TIFFGetField(tif, TIFFTAG_IMAGEDEPTH, &kFrst);
Z0=double(kFrst)*dx;
X0_={X0,Y0,Z0};
char * info;
if(TIFFGetField(tif, TIFFTAG_IMAGEDESCRIPTION, &info)) {
std::istringstream instrim(info);
while(instrim.good()) {
std::string str;
instrim>>str;
if(str=="dx") instrim >> dx_;
else if(str=="X0") instrim >> X0_;
}
}
if(dx_.x<1e-16) (std::cout<<"\n !!! Error: dx read from tif seems invalid !!! \n ").flush();
}
inline void setTifTags(const dbl3& X0_, const dbl3& dx_, TIFF *tif)
{ // negative numbers not supported in TIFFTAG_XPOSITION :-( for X0, using TIFFTAG_IMAGEDESCRIPTION instead
float dx=dx_[0], dy=dx_[1];
TIFFSetField(tif, TIFFTAG_XRESOLUTION, dx);
TIFFSetField(tif, TIFFTAG_YRESOLUTION, dy);
//TIFFSetField(tif, TIFFTAG_ZRESOLUTION, dz);, dz=dx_[2]
std::ostringstream ostrim;
ostrim<<" dx "<<dx_<<" ";
if(mag(X0_)>1e-16) ostrim<<" X0 "<<X0_<<" ";
(std::cout<<" tag: \""<<ostrim.str()<<"\" ").flush();
TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, ostrim.str().c_str());
TIFFSetField(tif, TIFFTAG_SOFTWARE, "voxelImage+libtiff");
}
template<typename T> int readTif( voxelField<T>& aa, std::string fnam )
{
(std::cout<< " reading tif file "<<fnam<<" ").flush();
uint32 nx, ny;
//uint16 samplesperpixel, bitspersample=1, config, photometric;
//uint16* red, green, blue;
//tsize_t rowsize;
//uint32 row;
//tsample_t s;
TIFF *tif = (TIFF *) NULL;
tif = TIFFOpen(fnam.c_str(), "r"); if (tif == NULL) { alert("Cannot open tif"); return -1; }
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &nx);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &ny);
//TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config);
int npages=TIFFNumberOfDirectories(tif);
npages=std::min(npages,maxNz);
(std::cout<<"size: "<<aa.size3()<<" * "<<sizeof(T)).flush();
{ voxelImageT<T>* vxls = dynamic_cast<voxelImageT<T>*>(&aa);
if(vxls) { getTifTags(vxls->X0Ch(),vxls->dxCh(),tif);
(std::cout<<", X0:"<<vxls->X0()<<", dx:"<<vxls->dx()).flush(); } }
aa.reset(nx,ny,npages,T(0.));
for(int pn=0;pn<npages;++pn) {
TIFFReadEncodedStrip( tif, static_cast<tstrip_t>(0), static_cast<void *>(&aa(0,0,pn)), static_cast<tsize_t>(nx * ny) * sizeof(T) );
//for (row = 0; row < ny; row++) { if (TIFFReadScanline(tif, &aa(0,row,pn), row, 0) < 0) break; }
TIFFReadDirectory(tif);
}
TIFFClose(tif);
std::cout<< " ."<<std::endl;
return 0;
}
template<typename T> int writeTif(const voxelField<T>& aa, std::string fnam) {
//uint32 rowsperstrip = uint32(-1);
uint32 nx=aa.nx(), ny=aa.ny();
int pn=0, npages=aa.nz();
int smplfrmt = tifDataType(T());
//uint16 samplesperpixel, bitspersample, config, photometric;
//uint16* red, green, blue;
//tsize_t rowsize;
//uint32 row;
//tsample_t s;
TIFF * tif = (TIFF *) NULL;
tif = TIFFOpen(fnam.c_str(), "w8"); if (tif == NULL) return -2;
const voxelImageT<T>* vxls = dynamic_cast<const voxelImageT<T>*>(&aa);
if(vxls) setTifTags(vxls->X0(),vxls->dx(),tif);
else std::cout<<"dxXo not set"<<std::endl;
//std::cout<<aa.size3()<<std::endl;
for(pn=0;pn<npages;++pn) {
//TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, nx);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, ny);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8*sizeof(T));
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
//TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
//cpTags(tif, tif);
TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, smplfrmt);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); ///. this is needed by stupid paraview!
if(sizeof(T)==1) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW); /// For best compression, choose COMPRESSION_PACKBITS and manually compress as 7z. Other opts:COMPRESSION_NONE COMPRESSION_DEFLATE COMPRESSION_PACKBITS
else TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
//TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, ny));
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, ny);
TIFFSetField(tif, TIFFTAG_PAGENUMBER, pn, npages);
T aOrig=aa(nx/2,ny/2,pn); //! Warn if image modified in libtiff
TIFFWriteEncodedStrip( tif, 0, const_cast<T *>(&aa(0,0,pn)), nx * ny * sizeof(T) );
if(aOrig!=aa(nx/2,ny/2,pn)) std::cout<<"Warning image modified in libtiff"<<std::endl;
//for (int row = 0; row<ny; row++) { if (TIFFWriteScanline(tif, &aa(0,row,pn), row, 0) < 0) break; }
TIFFWriteDirectory(tif);
}
TIFFClose(tif);
return (0);
}
template<typename T>
int writeTif(const voxelField<T>& aa, std::string fnam, int iStart,int iEnd , int jStart,int jEnd , int kStart,int kEnd ) {
voxelImageT<T> bb;
bb.reset({iEnd-iStart, jEnd-jStart, kEnd-kStart});
const voxelImageT<T>* vxls = dynamic_cast<const voxelImageT<T>*>(&aa);
if(vxls) bb.setFrom(*vxls, iStart, jStart, kStart);
else bb.voxelField<T>::setFrom(aa, iStart, jStart, kStart);
return writeTif(bb, fnam);
//! approach below complains about LZW compression.
//! Note although the deflate compression algorithm overal is a better choice,
//! for now we have to stick to LZW for portability
//! (specifically because Avizo does not support inflate algorithm).
uint32 rowsperstrip = uint32(-1);
TIFF *tif;
uint32 nx=iEnd-iStart, ny=jEnd-jStart;
int pn=0, npages=aa.nz();
int smplfrmt = tifDataType(T());
//uint16 samplesperpixel;
//uint16 bitspersample;
//uint16 config;
//uint16 photometric;
//uint16* red, green, blue;
//tsize_t rowsize;
int row;
//tsample_t s;
tif = (TIFF *) NULL;
tif = TIFFOpen(fnam.c_str(), "w8"); if (tif == NULL) return -2;
if(vxls) setTifTags(vxls->X0(),vxls->dx(),tif);
for(pn=0;pn<npages;++pn) {
TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, nx);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, ny);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8*sizeof(T));
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
//TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); ///. this is needed by stupid paraview!
TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, smplfrmt);
if(sizeof(T)==1) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW); /// For best compression, choose COMPRESSION_PACKBITS and manually compress as 7z. Other opts:COMPRESSION_NONE COMPRESSION_DEFLATE COMPRESSION_PACKBITS
else TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, rowsperstrip));
//TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, ny);
TIFFSetField(tif, TIFFTAG_PAGENUMBER, pn, npages);
T aOrig=aa(iStart+nx/2,jStart+ny/2,pn); //! Warn if image modified in libtiff
for (row = jStart; row < jEnd; row++) {
if (TIFFWriteScanline(tif, const_cast<T *>(&aa(iStart,row,kStart+pn)), row, 0) < 0)
break;
}
if(aOrig!=aa(iStart+nx/2,jStart+ny/2,pn)) std::cout<<"Warning image modified in libtiff"<<std::endl;
TIFFWriteDirectory(tif);
}
TIFFClose(tif);
return (0);
}
inline std::unique_ptr<voxelImageTBase> readTif(std::string fnam) {
TIFF *tif = (TIFF *) NULL;
tif = TIFFOpen(fnam.c_str(), "r"); if (tif == NULL) return std::unique_ptr<voxelImageTBase>();
uint16 frmt = SAMPLEFORMAT_UINT, nbits = 8;
TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &frmt);
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &nbits);
TIFFClose(tif);
std::cout<<"frmt: "<<frmt<<" : "<<nbits<<std::endl;
switch (frmt) {
case SAMPLEFORMAT_UINT:
default:
if (nbits==32) return std::make_unique<voxelImageT<unsigned int>>(fnam);
else if(nbits==16) return std::make_unique<voxelImageT<unsigned short>>(fnam);
else return std::make_unique<voxelImageT<unsigned char>>(fnam);
break;
case SAMPLEFORMAT_INT:
if (nbits==32) return std::make_unique<voxelImageT<int>>(fnam);
else if(nbits==16) return std::make_unique<voxelImageT<short>>(fnam);
else return std::make_unique<voxelImageT<char>>(fnam);
break;
case SAMPLEFORMAT_IEEEFP:
if (nbits==32) return std::make_unique<voxelImageT<float>>(fnam);
else return std::make_unique<voxelImageT<double>>(fnam);
break;
}
//TIFFDataType type=TIFF_BYTE;
//TIFFGetField(tif, TIFFTAG_DATATYPE, &type);
//std::cout<<type<<std::endl;
//switch (type) {
//switch (type) {
//case TIFF_BYTE:
//case TIFF_ASCII:
//case TIFF_UNDEFINED:
//default: return std::make_unique<voxelImageT<unsigned char>(fnam));
//case TIFF_SBYTE: return std::make_unique<voxelImageT<char>(fnam));
//break;
//case TIFF_SHORT: return std::make_unique<voxelImageT<unsigned short>(fnam));
//case TIFF_SSHORT: return std::make_unique<voxelImageT<short>(fnam));
//break;
//case TIFF_LONG: return std::make_unique<voxelImageT<unsigned int>(fnam));
//case TIFF_SLONG: return std::make_unique<voxelImageT<int>(fnam));
//case TIFF_IFD:
//case TIFF_RATIONAL:
//case TIFF_SRATIONAL:
//case TIFF_FLOAT: return std::make_unique<voxelImageT<float>(fnam));
//break;
//case TIFF_DOUBLE: return std::make_unique<voxelImageT<double>(fnam));
//break;
//}
}
#endif