forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkTIFFWriter.cxx
441 lines (399 loc) · 13 KB
/
vtkTIFFWriter.cxx
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTIFFWriter.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkTIFFWriter.h"
#include "vtkErrorCode.h"
#include "vtkImageData.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtk_tiff.h"
vtkStandardNewMacro(vtkTIFFWriter);
//----------------------------------------------------------------------------
vtkTIFFWriter::vtkTIFFWriter()
: TIFFPtr(nullptr), Compression(PackBits), Width(0), Height(0), Pages(0),
XResolution(-1.0), YResolution(-1.0)
{
}
//----------------------------------------------------------------------------
void vtkTIFFWriter::Write()
{
// make sure the latest input is available.
this->GetInputAlgorithm()->Update();
this->SetErrorCode(vtkErrorCode::NoError);
// Error checking
if (this->GetInput() == nullptr)
{
vtkErrorMacro(<<"Write: Please specify an input!");
return;
}
if (!this->FileName && !this->FilePattern)
{
vtkErrorMacro(<<"Write: Please specify either a FileName or a file prefix and pattern");
this->SetErrorCode(vtkErrorCode::NoFileNameError);
return;
}
// Make sure the file name is allocated - inherited from parent class,
// would be great to rewrite in more modern C++, but sticking with superclass
// for now to maintain behavior without rewriting/translating code.
size_t internalFileNameSize = (this->FileName ? strlen(this->FileName) : 1) +
(this->FilePrefix ? strlen(this->FilePrefix) : 1) +
(this->FilePattern ? strlen(this->FilePattern) : 1) + 256;
this->InternalFileName = new char[internalFileNameSize];
this->InternalFileName[0] = 0;
int bytesPrinted = 0;
// determine the name
if (this->FileName)
{
bytesPrinted = snprintf(this->InternalFileName, internalFileNameSize,
"%s",this->FileName);
}
else
{
if (this->FilePrefix)
{
bytesPrinted = snprintf(this->InternalFileName, internalFileNameSize,
this->FilePattern, this->FilePrefix, this->FileNumber);
}
else
{
bytesPrinted = snprintf(this->InternalFileName, internalFileNameSize,
this->FilePattern,this->FileNumber);
}
}
if (static_cast<size_t>(bytesPrinted) >= internalFileNameSize)
{
// Add null terminating character just to be safe.
this->InternalFileName[internalFileNameSize - 1] = 0;
vtkWarningMacro("Filename has been truncated.");
}
// Fill in image information.
this->GetInputExecutive(0, 0)->UpdateInformation();
int *wExtent;
wExtent = vtkStreamingDemandDrivenPipeline::GetWholeExtent(
this->GetInputInformation(0, 0));
this->FilesDeleted = 0;
this->UpdateProgress(0.0);
this->WriteFileHeader(nullptr, this->GetInput(), wExtent);
this->WriteFile(nullptr, this->GetInput(), wExtent, nullptr);
if (this->ErrorCode == vtkErrorCode::OutOfDiskSpaceError)
{
this->DeleteFiles();
}
else
{
this->WriteFileTrailer(nullptr, nullptr);
}
delete [] this->InternalFileName;
this->InternalFileName = nullptr;
}
//----------------------------------------------------------------------------
void vtkTIFFWriter::WriteFileHeader(ostream *, vtkImageData *data, int wExt[6])
{
int dims[3];
data->GetDimensions(dims);
int scomponents = data->GetNumberOfScalarComponents();
int stype = data->GetScalarType();
uint32 rowsperstrip = (uint32) -1;
int bps;
switch (stype)
{
case VTK_CHAR:
case VTK_SIGNED_CHAR:
case VTK_UNSIGNED_CHAR:
bps = 8;
break;
case VTK_SHORT:
case VTK_UNSIGNED_SHORT:
bps = 16;
break;
case VTK_FLOAT:
bps = 32;
break;
default:
vtkErrorMacro(<< "Unsupported data type: " << data->GetScalarTypeAsString());
this->SetErrorCode(vtkErrorCode::FileFormatError);
return;
}
int predictor;
// Find the width/height of the images
this->Width = wExt[1] - wExt[0] + 1;
this->Height = wExt[3] - wExt[2] + 1;
// Check if we need to write an image stack (pages > 2).
this->Pages = wExt[5] - wExt[4] + 1;
// Check the resolution too, assume we store it in metric (as in reader).
this->XResolution = 10.0 / data->GetSpacing()[0];
this->YResolution = 10.0 / data->GetSpacing()[1];
TIFF* tif = TIFFOpen(this->InternalFileName, "w");
if (!tif)
{
this->TIFFPtr = nullptr;
return;
}
this->TIFFPtr = tif;
// Let the volume do its metadata, keep existing for 2D images.
if (this->Pages > 1)
{
return;
}
uint32 w = this->Width;
uint32 h = this->Height;
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, w);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, h);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, scomponents);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps); // Fix for stype
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
if (stype == VTK_FLOAT)
{
TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
}
if ( scomponents > 3 )
{
// if number of scalar components is greater than 3, that means we assume
// there is alpha.
uint16 extra_samples = scomponents-3;
uint16 *sample_info = new uint16[scomponents-3];
sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
int cc;
for ( cc = 1; cc < scomponents-3; cc ++ )
{
sample_info[cc] = EXTRASAMPLE_UNSPECIFIED;
}
TIFFSetField(tif,TIFFTAG_EXTRASAMPLES,extra_samples,
sample_info);
delete [] sample_info;
}
int compression;
switch ( this->Compression )
{
case vtkTIFFWriter::PackBits: compression = COMPRESSION_PACKBITS; break;
case vtkTIFFWriter::JPEG: compression = COMPRESSION_JPEG; break;
case vtkTIFFWriter::Deflate: compression = COMPRESSION_DEFLATE; break;
case vtkTIFFWriter::LZW: compression = COMPRESSION_LZW; break;
default: compression = COMPRESSION_NONE;
}
//compression = COMPRESSION_JPEG;
TIFFSetField(tif, TIFFTAG_COMPRESSION, compression); // Fix for compression
uint16 photometric =
(scomponents == 1 ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB);
if ( compression == COMPRESSION_JPEG )
{
TIFFSetField(tif, TIFFTAG_JPEGQUALITY, 75); // Parameter
TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
photometric = PHOTOMETRIC_YCBCR;
}
else if ( compression == COMPRESSION_LZW )
{
predictor = 2;
TIFFSetField(tif, TIFFTAG_PREDICTOR, predictor);
vtkErrorMacro("LZW compression is patented outside US so it is disabled");
}
else if ( compression == COMPRESSION_DEFLATE )
{
predictor = 2;
TIFFSetField(tif, TIFFTAG_PREDICTOR, predictor);
}
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric); // Fix for scomponents
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,
TIFFDefaultStripSize(tif, rowsperstrip));
if (this->XResolution > 0.0 && this->YResolution > 0.0)
{
TIFFSetField(tif, TIFFTAG_XRESOLUTION, this->XResolution);
TIFFSetField(tif, TIFFTAG_YRESOLUTION, this->YResolution);
TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_CENTIMETER);
}
}
//----------------------------------------------------------------------------
void vtkTIFFWriter::WriteFile(ostream *, vtkImageData *data,
int extent[6], int*)
{
// Make sure we actually have data.
if (!data->GetPointData()->GetScalars())
{
vtkErrorMacro(<< "Could not get data from input.");
return;
}
TIFF* tif = reinterpret_cast<TIFF*>(this->TIFFPtr);
if (!tif)
{
vtkErrorMacro("Problem writing file.");
this->SetErrorCode(vtkErrorCode::FileFormatError);
return;
}
// take into consideration the scalar type
if( data->GetScalarType() != VTK_UNSIGNED_CHAR
&& data->GetScalarType() != VTK_UNSIGNED_SHORT
&& data->GetScalarType() != VTK_FLOAT)
{
vtkErrorMacro("TIFFWriter only accepts unsigned char/short or float scalars!");
return;
}
if (this->Pages > 1)
{
// Call the correct templated function for the input
void *inPtr = data->GetScalarPointer();
switch (data->GetScalarType())
{
vtkTemplateMacro(this->WriteVolume((VTK_TT *)(inPtr)));
default:
vtkErrorMacro("UpdateFromFile: Unknown data type");
}
}
else
{
// Now write the image for the current page/directory element.
int row = 0;
for (int idx2 = extent[4]; idx2 <= extent[5]; ++idx2)
{
for (int idx1 = extent[3]; idx1 >= extent[2]; idx1--)
{
void *ptr = data->GetScalarPointer(extent[0], idx1, idx2);
if (TIFFWriteScanline(tif, static_cast<unsigned char*>(ptr), row, 0) < 0)
{
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
break;
}
++row;
}
}
}
}
//----------------------------------------------------------------------------
template<typename T>
void vtkTIFFWriter::WriteVolume(T* buffer)
{
TIFF* tif = reinterpret_cast<TIFF*>(this->TIFFPtr);
if (!tif)
{
vtkErrorMacro("Problem writing volume.");
this->SetErrorCode(vtkErrorCode::FileFormatError);
return;
}
int width = this->Width;
int height = this->Height;
int pages = this->Pages;
uint32 w = width;
uint32 h = height;
int bitsPerSample = sizeof(T) * 8;
for (int page = 0; page < pages; ++page)
{
this->UpdateProgress(static_cast<double>(page + 1) / pages);
// TIFF directory set up/tags.
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, w);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, h);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bitsPerSample);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
int compression;
switch ( this->Compression )
{
case vtkTIFFWriter::PackBits:
compression = COMPRESSION_PACKBITS;
break;
case vtkTIFFWriter::JPEG:
compression = COMPRESSION_JPEG;
break;
case vtkTIFFWriter::Deflate:
compression = COMPRESSION_DEFLATE;
break;
case vtkTIFFWriter::LZW:
compression = COMPRESSION_LZW;
break;
default:
compression = COMPRESSION_NONE;
}
TIFFSetField(tif, TIFFTAG_COMPRESSION, compression); // Fix for compression
if (compression == COMPRESSION_LZW)
{
TIFFSetField(tif, TIFFTAG_PREDICTOR, 2);
vtkErrorMacro("LZW compression is patented outside US so it is disabled");
}
else if (compression == COMPRESSION_DEFLATE)
{
TIFFSetField(tif, TIFFTAG_PREDICTOR, 2);
}
if (bitsPerSample == 32)
{
TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
}
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
uint32 rowsperstrip = (uint32) -1;
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,
TIFFDefaultStripSize(tif, rowsperstrip));
if (this->XResolution > 0.0 && this->YResolution > 0.0)
{
TIFFSetField(tif, TIFFTAG_XRESOLUTION, this->XResolution);
TIFFSetField(tif, TIFFTAG_YRESOLUTION, this->YResolution);
TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_CENTIMETER);
}
// Extra pieces for multidirectory files.
TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
TIFFSetField(tif, TIFFTAG_PAGENUMBER, page, pages);
T* volume = buffer;
volume += width * height * page;
for (int i = 0; i < height; ++i)
{
T* tmp = volume + i * width;
if (TIFFWriteScanline(tif, reinterpret_cast<char*>(tmp), i, 0) < 0)
{
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return;
}
}
if (!TIFFWriteDirectory(tif))
{
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return;
}
}
}
//----------------------------------------------------------------------------
void vtkTIFFWriter::WriteFileTrailer(ostream *, vtkImageData *)
{
TIFF* tif = reinterpret_cast<TIFF*>(this->TIFFPtr);
if( tif)
{
TIFFClose(tif);
}
else
{
vtkErrorMacro("Problem writing trailer.");
this->SetErrorCode(vtkErrorCode::FileFormatError);
}
this->TIFFPtr = nullptr;
}
//----------------------------------------------------------------------------
void vtkTIFFWriter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Compression: ";
if ( this->Compression == vtkTIFFWriter::PackBits )
{
os << "Pack Bits\n";
}
else if ( this->Compression == vtkTIFFWriter::JPEG )
{
os << "JPEG\n";
}
else if ( this->Compression == vtkTIFFWriter::Deflate )
{
os << "Deflate\n";
}
else if ( this->Compression == vtkTIFFWriter::LZW )
{
os << "LZW\n";
}
else //if ( this->Compression == vtkTIFFWriter::NoCompression )
{
os << "No Compression\n";
}
}