vtkImageReader2.cxx 21.7 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1
2
3
4
5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkImageReader2.cxx

Brad King's avatar
Brad King committed
6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7
8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9

10
11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
Ken Martin's avatar
Ken Martin committed
13
14
15

=========================================================================*/
#include "vtkImageReader2.h"
16

Brad King's avatar
Brad King committed
17
#include "vtkByteSwap.h"
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
18
#include "vtkDataArray.h"
19
#include "vtkImageData.h"
20
21
#include "vtkInformation.h"
#include "vtkInformationVector.h"
22
#include "vtkObjectFactory.h"
23
#include "vtkPointData.h"
24
#include "vtkErrorCode.h"
25
#include "vtkStreamingDemandDrivenPipeline.h"
David Gobbi's avatar
David Gobbi committed
26
#include "vtkStringArray.h"
Ken Martin's avatar
Ken Martin committed
27

28
#include <sys/stat.h>
Ken Martin's avatar
Ken Martin committed
29

Brad King's avatar
Brad King committed
30
vtkStandardNewMacro(vtkImageReader2);
Ken Martin's avatar
Ken Martin committed
31
32
33
34
35
36
37
38
39
40
41
42
43

#ifdef read
#undef read
#endif

#ifdef close
#undef close
#endif

//----------------------------------------------------------------------------
vtkImageReader2::vtkImageReader2()
{
  this->FilePrefix = NULL;
44
45
  this->FilePattern = new char[strlen("%s.%d") + 1];
  strcpy (this->FilePattern, "%s.%d");
Ken Martin's avatar
Ken Martin committed
46
47
48
49
50
  this->File = NULL;

  this->DataScalarType = VTK_SHORT;
  this->NumberOfScalarComponents = 1;
  
51
  this->DataOrigin[0] = this->DataOrigin[1] = this->DataOrigin[2] = 0.0;
52

53
  this->DataSpacing[0] = this->DataSpacing[1] = this->DataSpacing[2] = 1.0;
54

55
56
  this->DataExtent[0] = this->DataExtent[2] = this->DataExtent[4] = 0;
  this->DataExtent[1] = this->DataExtent[3] = this->DataExtent[5] = 0;
57
58

  this->DataIncrements[0] = this->DataIncrements[1] = 
59
  this->DataIncrements[2] = this->DataIncrements[3] = 1;
Ken Martin's avatar
Ken Martin committed
60
  
David Gobbi's avatar
David Gobbi committed
61
62
  this->FileNames = NULL;

Ken Martin's avatar
Ken Martin committed
63
64
65
66
67
68
  this->FileName = NULL;
  this->InternalFileName = NULL;
  
  this->HeaderSize = 0;
  this->ManualHeaderSize = 0;
  
69
70
  this->FileNameSliceOffset = 0;
  this->FileNameSliceSpacing = 1;
71

Ken Martin's avatar
Ken Martin committed
72
73
74
  // Left over from short reader
  this->SwapBytes = 0;
  this->FileLowerLeft = 0;
75
  this->FileDimensionality = 2;
76
  this->SetNumberOfInputPorts(0);
Ken Martin's avatar
Ken Martin committed
77
78
79
80
}

//----------------------------------------------------------------------------
vtkImageReader2::~vtkImageReader2()
David Gobbi's avatar
David Gobbi committed
81
{
Ken Martin's avatar
Ken Martin committed
82
83
84
85
86
87
88
  if (this->File)
    {
    this->File->close();
    delete this->File;
    this->File = NULL;
    }
  
David Gobbi's avatar
David Gobbi committed
89
90
91
92
93
  if (this->FileNames)
    {
    this->FileNames->Delete();
    this->FileNames = NULL;
    }
Ken Martin's avatar
Ken Martin committed
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
  if (this->FileName)
    {
    delete [] this->FileName;
    this->FileName = NULL;
    }
  if (this->FilePrefix)
    {
    delete [] this->FilePrefix;
    this->FilePrefix = NULL;
    }
  if (this->FilePattern)
    {
    delete [] this->FilePattern;
    this->FilePattern = NULL;
    }
  if (this->InternalFileName)
    {
    delete [] this->InternalFileName;
    this->InternalFileName = NULL;
    }
}

//----------------------------------------------------------------------------
// This function sets the name of the file. 
void vtkImageReader2::ComputeInternalFileName(int slice)
{
  // delete any old filename
  if (this->InternalFileName)
    {
    delete [] this->InternalFileName;
124
    this->InternalFileName = NULL;
Ken Martin's avatar
Ken Martin committed
125
126
    }
  
David Gobbi's avatar
David Gobbi committed
127
  if (!this->FileName && !this->FilePattern && !this->FileNames)
Ken Martin's avatar
Ken Martin committed
128
    {
David Gobbi's avatar
David Gobbi committed
129
130
    vtkErrorMacro(<<"Either a FileName, FileNames, or FilePattern"
                  <<" must be specified.");
Ken Martin's avatar
Ken Martin committed
131
132
133
134
    return;
    }

  // make sure we figure out a filename to open
David Gobbi's avatar
David Gobbi committed
135
136
137
138
139
140
141
  if (this->FileNames)
    {
    const char *filename = this->FileNames->GetValue(slice);
    this->InternalFileName = new char [strlen(filename) + 10];
    sprintf(this->InternalFileName,"%s",filename);
    }
  else if (this->FileName)
Ken Martin's avatar
Ken Martin committed
142
143
144
145
146
147
    {
    this->InternalFileName = new char [strlen(this->FileName) + 10];
    sprintf(this->InternalFileName,"%s",this->FileName);
    }
  else 
    {
148
149
150
    int slicenum = 
      slice * this->FileNameSliceSpacing
      + this->FileNameSliceOffset;
Bill Hoffman's avatar
Bill Hoffman committed
151
    if (this->FilePrefix && this->FilePattern)
Ken Martin's avatar
Ken Martin committed
152
153
154
155
      {
      this->InternalFileName = new char [strlen(this->FilePrefix) +
                                        strlen(this->FilePattern) + 10];
      sprintf (this->InternalFileName, this->FilePattern, 
156
               this->FilePrefix, slicenum);
Ken Martin's avatar
Ken Martin committed
157
      }
Bill Hoffman's avatar
Bill Hoffman committed
158
    else if (this->FilePattern)
Ken Martin's avatar
Ken Martin committed
159
160
      {
      this->InternalFileName = new char [strlen(this->FilePattern) + 10];
161
      int len = static_cast<int>(strlen(this->FilePattern));
162
163
164
      int hasPercentS = 0;
      for(int i =0; i < len-1; ++i)
        {
Bill Hoffman's avatar
Bill Hoffman committed
165
        if(this->FilePattern[i] == '%' && this->FilePattern[i+1] == 's')
166
167
168
169
170
171
172
173
174
175
176
177
178
          {
          hasPercentS = 1;
          break;
          }
        }
      if(hasPercentS)
        {
        sprintf (this->InternalFileName, this->FilePattern, "", slicenum);
        }
      else
        {
        sprintf (this->InternalFileName, this->FilePattern, slicenum);
        }
Ken Martin's avatar
Ken Martin committed
179
      }
Bill Hoffman's avatar
Bill Hoffman committed
180
181
182
183
184
    else
      {
      delete [] this->InternalFileName;
      this->InternalFileName = 0;
      }
Ken Martin's avatar
Ken Martin committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    }
}


//----------------------------------------------------------------------------
// This function sets the name of the file. 
void vtkImageReader2::SetFileName(const char *name)
{
  if ( this->FileName && name && (!strcmp(this->FileName,name)))
    {
    return;
    }
  if (!name && !this->FileName)
    {
    return;
    }
  if (this->FileName)
    {
    delete [] this->FileName;
David Gobbi's avatar
David Gobbi committed
204
    this->FileName = NULL;
Ken Martin's avatar
Ken Martin committed
205
206
207
208
209
    }
  if (name)
    {
    this->FileName = new char[strlen(name) + 1];
    strcpy(this->FileName, name);
David Gobbi's avatar
David Gobbi committed
210
211
212
213
214
215
216
217
218
219
220

    if (this->FilePrefix)
      {
      delete [] this->FilePrefix;
      this->FilePrefix = NULL;
      }
    if (this->FileNames)
      {
      this->FileNames->Delete();
      this->FileNames = NULL;
      }
Ken Martin's avatar
Ken Martin committed
221
    }
David Gobbi's avatar
David Gobbi committed
222
223
224
225
226
227
228
229
230

  this->Modified();
}

//----------------------------------------------------------------------------
// This function sets an array containing file names 
void vtkImageReader2::SetFileNames(vtkStringArray *filenames)
{
  if (filenames == this->FileNames)
Ken Martin's avatar
Ken Martin committed
231
    {
David Gobbi's avatar
David Gobbi committed
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
    return;
    }
  if (this->FileNames)
    {
    this->FileNames->Delete();
    this->FileNames = 0;
    }
  if (filenames)
    {
    this->FileNames = filenames;
    this->FileNames->Register(this);
    if (this->FileNames->GetNumberOfValues() > 0)
      {
      this->DataExtent[4] = 0;
      this->DataExtent[5] = this->FileNames->GetNumberOfValues() - 1;
      }
    if (this->FilePrefix)
      {
      delete [] this->FilePrefix;
      this->FilePrefix = NULL;
      }
    if (this->FileName)
      {
      delete [] this->FileName;
      this->FileName = NULL;
      }
Ken Martin's avatar
Ken Martin committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
    }

  this->Modified();
}

//----------------------------------------------------------------------------
// This function sets the prefix of the file name. "image" would be the
// name of a series: image.1, image.2 ...
void vtkImageReader2::SetFilePrefix(const char *prefix)
{
  if ( this->FilePrefix && prefix && (!strcmp(this->FilePrefix,prefix)))
    {
    return;
    }
  if (!prefix && !this->FilePrefix)
    {
    return;
    }
  if (this->FilePrefix)
    {
    delete [] this->FilePrefix;
David Gobbi's avatar
David Gobbi committed
279
    this->FilePrefix = NULL;
Ken Martin's avatar
Ken Martin committed
280
    }
David Gobbi's avatar
David Gobbi committed
281
  if (prefix)
Ken Martin's avatar
Ken Martin committed
282
    {
David Gobbi's avatar
David Gobbi committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
    this->FilePrefix = new char[strlen(prefix) + 1];
    strcpy(this->FilePrefix, prefix);

    if (this->FileName)
      {
      delete [] this->FileName;
      this->FileName = NULL;
      }
    if (this->FileNames)
      {
      this->FileNames->Delete();
      this->FileNames = NULL;
      }
    }

Ken Martin's avatar
Ken Martin committed
298
299
300
301
302
  this->Modified();
}

//----------------------------------------------------------------------------
// This function sets the pattern of the file name which turn a prefix
Mathieu Malaterre's avatar
Mathieu Malaterre committed
303
// into a file name. "%s.%03d" would be the
Ken Martin's avatar
Ken Martin committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// pattern of a series: image.001, image.002 ...
void vtkImageReader2::SetFilePattern(const char *pattern)
{
  if ( this->FilePattern && pattern && 
       (!strcmp(this->FilePattern,pattern)))
    {
    return;
    }
  if (!pattern && !this->FilePattern)
    {
    return;
    }
  if (this->FilePattern)
    {
    delete [] this->FilePattern;
David Gobbi's avatar
David Gobbi committed
319
    this->FilePattern = NULL;
Ken Martin's avatar
Ken Martin committed
320
    }
David Gobbi's avatar
David Gobbi committed
321
  if (pattern)
Ken Martin's avatar
Ken Martin committed
322
    {
David Gobbi's avatar
David Gobbi committed
323
324
325
326
327
328
329
330
331
332
333
334
335
    this->FilePattern = new char[strlen(pattern) + 1];
    strcpy(this->FilePattern, pattern);

    if (this->FileName)
      {
      delete [] this->FileName;
      this->FileName = NULL;
      }
    if (this->FileNames)
      {
      this->FileNames->Delete();
      this->FileNames = NULL;
      }
Ken Martin's avatar
Ken Martin committed
336
    }
David Gobbi's avatar
David Gobbi committed
337

Ken Martin's avatar
Ken Martin committed
338
339
340
  this->Modified();
}

341
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
342
343
344
345
346
347
348
349
350
void vtkImageReader2::SetDataByteOrderToBigEndian()
{
#ifndef VTK_WORDS_BIGENDIAN
  this->SwapBytesOn();
#else
  this->SwapBytesOff();
#endif
}

351
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
352
353
354
355
356
357
358
359
360
void vtkImageReader2::SetDataByteOrderToLittleEndian()
{
#ifdef VTK_WORDS_BIGENDIAN
  this->SwapBytesOn();
#else
  this->SwapBytesOff();
#endif
}

361
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
362
363
364
365
366
367
368
369
370
371
372
373
void vtkImageReader2::SetDataByteOrder(int byteOrder)
{
  if ( byteOrder == VTK_FILE_BYTE_ORDER_BIG_ENDIAN )
    {
    this->SetDataByteOrderToBigEndian();
    }
  else
    {
    this->SetDataByteOrderToLittleEndian();
    }
}

374
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
int vtkImageReader2::GetDataByteOrder()
{
#ifdef VTK_WORDS_BIGENDIAN
  if ( this->SwapBytes )
    {
    return VTK_FILE_BYTE_ORDER_LITTLE_ENDIAN;
    }
  else
    {
    return VTK_FILE_BYTE_ORDER_BIG_ENDIAN;
    }
#else
  if ( this->SwapBytes )
    {
    return VTK_FILE_BYTE_ORDER_BIG_ENDIAN;
    }
  else
    {
    return VTK_FILE_BYTE_ORDER_LITTLE_ENDIAN;
    }
#endif
}

398
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
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
const char *vtkImageReader2::GetDataByteOrderAsString()
{
#ifdef VTK_WORDS_BIGENDIAN
  if ( this->SwapBytes )
    {
    return "LittleEndian";
    }
  else
    {
    return "BigEndian";
    }
#else
  if ( this->SwapBytes )
    {
    return "BigEndian";
    }
  else
    {
    return "LittleEndian";
    }
#endif
}


//----------------------------------------------------------------------------
void vtkImageReader2::PrintSelf(ostream& os, vtkIndent indent)
{
  int idx;
  
Brad King's avatar
Brad King committed
428
  this->Superclass::PrintSelf(os,indent);
Ken Martin's avatar
Ken Martin committed
429
430
431
432

  // this->File, this->Colors need not be printed  
  os << indent << "FileName: " <<
    (this->FileName ? this->FileName : "(none)") << "\n";
David Gobbi's avatar
David Gobbi committed
433
  os << indent << "FileNames: " << this->FileNames << "\n";
Ken Martin's avatar
Ken Martin committed
434
435
436
437
438
  os << indent << "FilePrefix: " << 
    (this->FilePrefix ? this->FilePrefix : "(none)") << "\n";
  os << indent << "FilePattern: " << 
    (this->FilePattern ? this->FilePattern : "(none)") << "\n";

439
440
441
442
443
  os << indent << "FileNameSliceOffset: " 
     << this->FileNameSliceOffset << "\n";
  os << indent << "FileNameSliceSpacing: " 
     << this->FileNameSliceSpacing << "\n";

Ken Martin's avatar
Ken Martin committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  os << indent << "DataScalarType: " 
     << vtkImageScalarTypeNameMacro(this->DataScalarType) << "\n";
  os << indent << "NumberOfScalarComponents: " 
     << this->NumberOfScalarComponents << "\n";
 
  os << indent << "File Dimensionality: " << this->FileDimensionality << "\n";

  os << indent << "File Lower Left: " << 
    (this->FileLowerLeft ? "On\n" : "Off\n");

  os << indent << "Swap Bytes: " << (this->SwapBytes ? "On\n" : "Off\n");

  os << indent << "DataIncrements: (" << this->DataIncrements[0];
  for (idx = 1; idx < 2; ++idx)
    {
    os << ", " << this->DataIncrements[idx];
    }
  os << ")\n";
  
  os << indent << "DataExtent: (" << this->DataExtent[0];
  for (idx = 1; idx < 6; ++idx)
    {
    os << ", " << this->DataExtent[idx];
    }
  os << ")\n";
  
  os << indent << "DataSpacing: (" << this->DataSpacing[0];
  for (idx = 1; idx < 3; ++idx)
    {
    os << ", " << this->DataSpacing[idx];
    }
  os << ")\n";
  
  os << indent << "DataOrigin: (" << this->DataOrigin[0];
  for (idx = 1; idx < 3; ++idx)
    {
    os << ", " << this->DataOrigin[idx];
    }
  os << ")\n";
  
  os << indent << "HeaderSize: " << this->HeaderSize << "\n";

  if ( this->InternalFileName )
    {
    os << indent << "Internal File Name: " << this->InternalFileName << "\n";
    }
  else
    {
    os << indent << "Internal File Name: (none)\n";
    }
}

496
//----------------------------------------------------------------------------
497
498
499
500
501
502
503
void vtkImageReader2::ExecuteInformation()
{
  // this is empty, the idea is that converted filters should implement
  // RequestInformation. But to help out old filters we will call
  // ExecuteInformation and hope that the subclasses correctly set the ivars
  // and not the output.
}
Ken Martin's avatar
Ken Martin committed
504
505
506

//----------------------------------------------------------------------------
// This method returns the largest data that can be generated.
507
int vtkImageReader2::RequestInformation (
508
509
510
  vtkInformation       * vtkNotUsed( request ),
  vtkInformationVector** vtkNotUsed( inputVector ),
  vtkInformationVector * outputVector)
Ken Martin's avatar
Ken Martin committed
511
{
512
  this->SetErrorCode( vtkErrorCode::NoError );
513
514
  // call for backwards compatibility
  this->ExecuteInformation();
515
516
517
518
519
520
  // Check for any error set by downstream filter (IO in most case)
  if ( this->GetErrorCode() )
    {
    return 0;
    }

521
522
  // get the info objects
  vtkInformation* outInfo = outputVector->GetInformationObject(0);
David Gobbi's avatar
David Gobbi committed
523
524
525
526
527
528
529

  // if a list of file names is supplied, set slice extent
  if (this->FileNames && this->FileNames->GetNumberOfValues() > 0)
    {
    this->DataExtent[4] = 0;
    this->DataExtent[5] = this->FileNames->GetNumberOfValues()-1;
    }
530
531
532
533
534
 
  outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
               this->DataExtent, 6);
  outInfo->Set(vtkDataObject::SPACING(), this->DataSpacing, 3);
  outInfo->Set(vtkDataObject::ORIGIN(),  this->DataOrigin, 3);
Ken Martin's avatar
Ken Martin committed
535

536
537
  vtkDataObject::SetPointDataActiveScalarInfo(outInfo, this->DataScalarType,
    this->NumberOfScalarComponents);
538
539
  return 1;
}
Ken Martin's avatar
Ken Martin committed
540
541
542

//----------------------------------------------------------------------------
// Manual initialization.
543
void vtkImageReader2::SetHeaderSize(unsigned long size)
Ken Martin's avatar
Ken Martin committed
544
545
546
547
548
549
550
551
552
553
{
  if (size != this->HeaderSize)
    {
    this->HeaderSize = size;
    this->Modified();
    }
  this->ManualHeaderSize = 1;
}
  

554
555
556
557
558
559
560
//----------------------------------------------------------------------------
template <class T>
unsigned long vtkImageReader2GetSize(T*)
{
  return sizeof(T);
}

Ken Martin's avatar
Ken Martin committed
561
562
563
564
565
566
567
568
569
570
571
//----------------------------------------------------------------------------
// This function opens a file to determine the file size, and to
// automatically determine the header size.
void vtkImageReader2::ComputeDataIncrements()
{
  int idx;
  unsigned long fileDataLength;
  
  // Determine the expected length of the data ...
  switch (this->DataScalarType)
    {
572
573
574
    vtkTemplateMacro(
      fileDataLength = vtkImageReader2GetSize(static_cast<VTK_TT*>(0))
      );
Ken Martin's avatar
Ken Martin committed
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
    default:
      vtkErrorMacro(<< "Unknown DataScalarType");
      return;
    }

  fileDataLength *= this->NumberOfScalarComponents;
  
  // compute the fileDataLength (in units of bytes)
  for (idx = 0; idx < 3; ++idx)
    {
    this->DataIncrements[idx] = fileDataLength;
    fileDataLength = fileDataLength *
      (this->DataExtent[idx*2+1] - this->DataExtent[idx*2] + 1);
    }
  this->DataIncrements[3] = fileDataLength;
}


593
//----------------------------------------------------------------------------
594
int vtkImageReader2::OpenFile()
Ken Martin's avatar
Ken Martin committed
595
596
597
{
  if (!this->FileName && !this->FilePattern)
    {
David Gobbi's avatar
David Gobbi committed
598
599
    vtkErrorMacro(<<"Either a FileName, FileNames, or FilePattern"
                  << " must be specified.");
600
    return 0;
Ken Martin's avatar
Ken Martin committed
601
602
603
604
605
606
607
608
609
610
611
612
    }

  // Close file from any previous image
  if (this->File)
    {
    this->File->close();
    delete this->File;
    this->File = NULL;
    }
  
  // Open the new file
  vtkDebugMacro(<< "Initialize: opening file " << this->InternalFileName);
613
614
615
  struct stat fs;
  if ( !stat( this->InternalFileName, &fs) )
    {
Ken Martin's avatar
Ken Martin committed
616
#ifdef _WIN32
617
    this->File = new ifstream(this->InternalFileName, ios::in | ios::binary);
Ken Martin's avatar
Ken Martin committed
618
#else
619
    this->File = new ifstream(this->InternalFileName, ios::in);
Ken Martin's avatar
Ken Martin committed
620
#endif
621
    }
Ken Martin's avatar
Ken Martin committed
622
623
  if (! this->File || this->File->fail())
    {
David Gobbi's avatar
David Gobbi committed
624
625
    vtkErrorMacro(<< "Initialize: Could not open file "
                  << this->InternalFileName);
626
    return 0;
Ken Martin's avatar
Ken Martin committed
627
    }
628
  return 1;
Ken Martin's avatar
Ken Martin committed
629
630
631
}


Mathieu Malaterre's avatar
Mathieu Malaterre committed
632
//----------------------------------------------------------------------------
633
unsigned long vtkImageReader2::GetHeaderSize()
Ken Martin's avatar
Ken Martin committed
634
{
David Gobbi's avatar
David Gobbi committed
635
636
637
638
639
640
641
642
643
644
645
646
647
648
  unsigned long firstIdx;

  if (this->FileNames)
    {
    // if FileNames is used, indexing always starts at zero
    firstIdx = 0;
    }
  else
    {
    // FilePrefix uses the DataExtent to figure out the first slice index
    firstIdx = this->DataExtent[4];
    }

  return this->GetHeaderSize(firstIdx);
Ken Martin's avatar
Ken Martin committed
649
650
}

Mathieu Malaterre's avatar
Mathieu Malaterre committed
651
//----------------------------------------------------------------------------
652
unsigned long vtkImageReader2::GetHeaderSize(unsigned long idx)
Ken Martin's avatar
Ken Martin committed
653
654
655
656
657
658
659
660
661
662
663
664
{
  if (!this->FileName && !this->FilePattern)
    {
    vtkErrorMacro(<<"Either a FileName or FilePattern must be specified.");
    return 0;
    }
  if ( ! this->ManualHeaderSize)
    {
    this->ComputeDataIncrements();

    // make sure we figure out a filename to open
    this->ComputeInternalFileName(idx);
665
666

    struct stat statbuf;
Ken Martin's avatar
Ken Martin committed
667
668
669
670
671
    if (!stat(this->InternalFileName, &statbuf))
      {
      return (int)(statbuf.st_size -
                   (long)this->DataIncrements[this->GetFileDimensionality()]);
      }
Ken Martin's avatar
Ken Martin committed
672
673
674
675
676
    }
  
  return this->HeaderSize;
}

Mathieu Malaterre's avatar
Mathieu Malaterre committed
677
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
void vtkImageReader2::SeekFile(int i, int j, int k)
{
  unsigned long streamStart;

  // convert data extent into constants that can be used to seek.
  streamStart = 
    (i - this->DataExtent[0]) * this->DataIncrements[0];
  
  if (this->FileLowerLeft)
    {
    streamStart = streamStart + 
      (j - this->DataExtent[2]) * this->DataIncrements[1];
    }
  else
    {
    streamStart = streamStart + 
      (this->DataExtent[3] - this->DataExtent[2] - j) * 
      this->DataIncrements[1];
    }
  
  // handle three and four dimensional files
  if (this->GetFileDimensionality() >= 3)
    {
    streamStart = streamStart + 
      (k - this->DataExtent[4]) * this->DataIncrements[2];
    }
  
  streamStart += this->GetHeaderSize(k);
  
  // error checking
Sean McBride's avatar
Sean McBride committed
708
709
710
711
712
713
  if (!this->File)
    {
    vtkWarningMacro(<<"File must be specified.");
    return;
    }

Ken Martin's avatar
Ken Martin committed
714
715
716
717
718
719
720
721
722
723
724
725
  this->File->seekg((long)streamStart, ios::beg);
  if (this->File->fail())
    {
    vtkWarningMacro("File operation failed.");
    return;
    }
}

//----------------------------------------------------------------------------
// This function reads in one data of data.
// templated to handle different data types.
template <class OT>
Ken Martin's avatar
Ken Martin committed
726
void vtkImageReader2Update(vtkImageReader2 *self, vtkImageData *data, OT *outPtr)
Ken Martin's avatar
Ken Martin committed
727
{
728
  vtkIdType outIncr[3];
Ken Martin's avatar
Ken Martin committed
729
730
  OT *outPtr1, *outPtr2;
  long streamRead;
Ken Martin's avatar
Ken Martin committed
731
  int idx1, idx2, nComponents;
Ken Martin's avatar
Ken Martin committed
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
  int outExtent[6];
  unsigned long count = 0;
  unsigned long target;
  
  // Get the requested extents and increments
  data->GetExtent(outExtent);
  data->GetIncrements(outIncr);
  nComponents = data->GetNumberOfScalarComponents();
  
  // length of a row, num pixels read at a time
  int pixelRead = outExtent[1] - outExtent[0] + 1; 
  streamRead = (long)(pixelRead*nComponents*sizeof(OT));  
  
  // create a buffer to hold a row of the data
  target = (unsigned long)((outExtent[5]-outExtent[4]+1)*
747
                           (outExtent[3]-outExtent[2]+1)/50.0);
Ken Martin's avatar
Ken Martin committed
748
749
750
751
752
753
  target++;

  // read the data row by row
  if (self->GetFileDimensionality() == 3)
    {
    self->ComputeInternalFileName(0);
754
755
756
757
    if ( !self->OpenFile() )
      {
      return;
      }
Ken Martin's avatar
Ken Martin committed
758
759
760
761
762
763
764
    }
  outPtr2 = outPtr;
  for (idx2 = outExtent[4]; idx2 <= outExtent[5]; ++idx2)
    {
    if (self->GetFileDimensionality() == 2)
      {
      self->ComputeInternalFileName(idx2);
765
766
767
768
      if ( !self->OpenFile() )
        {
        return;
        }
Ken Martin's avatar
Ken Martin committed
769
770
771
      }
    outPtr1 = outPtr2;
    for (idx1 = outExtent[2]; 
772
         !self->AbortExecute && idx1 <= outExtent[3]; ++idx1)
Ken Martin's avatar
Ken Martin committed
773
774
      {
      if (!(count%target))
775
776
777
        {
        self->UpdateProgress(count/(50.0*target));
        }
Ken Martin's avatar
Ken Martin committed
778
779
780
781
782
783
      count++;
      
      // seek to the correct row
      self->SeekFile(outExtent[0],idx1,idx2);
      // read the row.
      if ( !self->GetFile()->read((char *)outPtr1, streamRead))
784
785
786
        {
        vtkGenericWarningMacro("File operation failed. row = " << idx1
                               << ", Read = " << streamRead
787
                               << ", FilePos = " << static_cast<vtkIdType>(self->GetFile()->tellg()));
788
789
        return;
        }
Ken Martin's avatar
Ken Martin committed
790
791
      // handle swapping
      if (self->GetSwapBytes() && sizeof(OT) > 1)
792
793
794
        {
        vtkByteSwap::SwapVoidRange(outPtr1, pixelRead*nComponents, sizeof(OT));
        }
Ken Martin's avatar
Ken Martin committed
795
796
797
798
799
800
801
802
803
804
805
806
807
808
      outPtr1 += outIncr[1];
      }
    // move to the next image in the file and data
    outPtr2 += outIncr[2];
    }
}

//----------------------------------------------------------------------------
// This function reads a data from a file.  The datas extent/axes
// are assumed to be the same as the file extent/order.
void vtkImageReader2::ExecuteData(vtkDataObject *output)
{
  vtkImageData *data = this->AllocateOutputData(output);
  
809
  void *ptr;
Ken Martin's avatar
Ken Martin committed
810
811
  int *ext;
  
812
  if (!this->FileName && !this->FilePattern)
Ken Martin's avatar
Ken Martin committed
813
    {
814
    vtkErrorMacro("Either a valid FileName or FilePattern must be specified.");
Ken Martin's avatar
Ken Martin committed
815
816
817
818
819
    return;
    }

  ext = data->GetExtent();

820
821
  data->GetPointData()->GetScalars()->SetName("ImageFile");

Ken Martin's avatar
Ken Martin committed
822
  vtkDebugMacro("Reading extent: " << ext[0] << ", " << ext[1] << ", " 
823
        << ext[2] << ", " << ext[3] << ", " << ext[4] << ", " << ext[5]);
Ken Martin's avatar
Ken Martin committed
824
825
826
827
828
829
830
  
  this->ComputeDataIncrements();
  
  // Call the correct templated function for the output
  ptr = data->GetScalarPointer();
  switch (this->GetDataScalarType())
    {
831
    vtkTemplateMacro(vtkImageReader2Update(this, data, (VTK_TT *)(ptr)));
Ken Martin's avatar
Ken Martin committed
832
833
834
835
836
837
838
    default:
      vtkErrorMacro(<< "UpdateFromFile: Unknown data type");
    }   
}


//----------------------------------------------------------------------------
839
// Set the data type of pixels in the file.  
Ken Martin's avatar
Ken Martin committed
840
841
842
843
844
845
846
847
848
849
850
851
852
853
// If you want the output scalar type to have a different value, set it
// after this method is called.
void vtkImageReader2::SetDataScalarType(int type)
{
  if (type == this->DataScalarType)
    {
    return;
    }
  
  this->Modified();
  this->DataScalarType = type;
  // Set the default output scalar type
  this->GetOutput()->SetScalarType(this->DataScalarType);
}