File: itkVoxBoCUBImageIO.cxx

package info (click to toggle)
itksnap 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,196 kB
  • ctags: 9,196
  • sloc: cpp: 62,895; sh: 175; makefile: 13
file content (837 lines) | stat: -rw-r--r-- 25,558 bytes parent folder | download | duplicates (2)
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
442
443
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
/*=========================================================================

  Program:   ITK-SNAP
  Module:    $RCSfile: itkVoxBoCUBImageIO.cxx,v $
  Language:  C++
  Date:      $Date: 2008/11/20 04:23:28 $
  Version:   $Revision: 1.4 $  

  Copyright (c) Insight Software Consortium. All rights reserved.
  
  This file is part of ITK-SNAP 

  ITK-SNAP is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

  -----

  Copyright (c) 2003 Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/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 notices for more information. 


=========================================================================*/
#include "itkVoxBoCUBImageIO.h"
#include "itkIOCommon.h"
#include "itkExceptionObject.h"
#include "itkMetaDataObject.h"
#include "itkByteSwapper.h"
#include <iostream>
#include <list>
#include <string>
#include <math.h>

// Commented out because zlib is not available through Insight Applications
#ifdef SNAP_GZIP_SUPPORT
#include <zlib.h>
#endif
#include "itkSpatialOrientationAdapter.h"

namespace itk {


/**
 * A generic reader and writer object for VoxBo files. Basically it
 * provides uniform access to gzip and normal files
 */
class GenericCUBFileAdaptor
{
public:
    virtual ~GenericCUBFileAdaptor() {}
  virtual unsigned char ReadByte() = 0;
  virtual void ReadData(void *data, unsigned long bytes) = 0;
  virtual void WriteData(const void *data, unsigned long bytes) = 0;

  std::string ReadHeader()
    {
    // Read everything up to the \f symbol
    std::ostringstream oss;
    unsigned char byte = ReadByte();
    while(byte != '\f')
      {
      oss << byte;
      byte = ReadByte();
      }

    // Read the next byte
    unsigned char term = ReadByte();
    if(term == '\r')
      term = ReadByte();

    // Throw exception if term is not there
    if(term != '\n')
      {
      ExceptionObject exception;
      exception.SetDescription("Header is not terminated by newline.");
      throw exception;
      }

    // Return the header string
    return oss.str();
    }
};

/**
 * A reader for gzip files
 */
#ifdef SNAP_GZIP_SUPPORT

class CompressedCUBFileAdaptor : public GenericCUBFileAdaptor
{
public:
  CompressedCUBFileAdaptor(const char *file, const char *mode)
    {
    m_GzFile = ::gzopen(file, mode);
    if(m_GzFile == NULL)
      {
      ExceptionObject exception;
      exception.SetDescription("File cannot be accessed");
      throw exception;
      }
    }
  
  ~CompressedCUBFileAdaptor()
    {
    if(m_GzFile)
      ::gzclose(m_GzFile);
    }

  unsigned char ReadByte()
    {
    int byte = ::gzgetc(m_GzFile);
    if(byte < 0)
      {
      std::ostringstream oss;
      oss << "Error reading byte from file at position: " << ::gztell(m_GzFile);
      ExceptionObject exception;
      exception.SetDescription(oss.str().c_str());
      throw exception;
      }
    return static_cast<unsigned char>(byte);
    }
  
  void ReadData(void *data, unsigned long bytes)
    {
    if(m_GzFile == NULL)
      {
      ExceptionObject exception;
      exception.SetDescription("File cannot be read");
      throw exception;
      }

    int bread = ::gzread(m_GzFile, data, bytes);
    if(bread != bytes)
      {
      std::ostringstream oss;
      oss << "File size does not match header: " 
        << bytes << " bytes requested but only "
        << bread << " bytes available!" << std::endl
        << "At file position " << ::gztell(m_GzFile);
      ExceptionObject exception;
      exception.SetDescription(oss.str().c_str());
      throw exception;
      }
    }
  
  void WriteData(const void *data, unsigned long bytes)
    {
    if(m_GzFile == NULL)
      {
      ExceptionObject exception;
      exception.SetDescription("File cannot be written");
      throw exception;
      }

    int bwritten = ::gzwrite(m_GzFile, (void *) data, bytes);
    if(bwritten != bytes)
      {
      ExceptionObject exception;
      exception.SetDescription("Could not write all bytes to file");
      throw exception;
      }
    }

private:
  ::gzFile m_GzFile;
};

#endif // SNAP_GZIP_SUPPORT

/**
 * A reader for non-gzip files
 */
class DirectCUBFileAdaptor : public GenericCUBFileAdaptor
{
public:
  DirectCUBFileAdaptor(const char *file, const char *mode)
    {
    m_File = fopen(file, mode);
    if(m_File == NULL)
      {
      ExceptionObject exception;
      exception.SetDescription("File cannot be read");
      throw exception;
      }
    }

  virtual ~DirectCUBFileAdaptor()
    {
    if(m_File)
      fclose(m_File);
    }

  unsigned char ReadByte()
    {
    int byte = fgetc(m_File);
    if(byte == EOF)
      {
      std::ostringstream oss;
      oss << "Error reading byte from file at position: " << ::ftell(m_File);
      ExceptionObject exception;
      exception.SetDescription(oss.str().c_str());
      throw exception;
      }
    return static_cast<unsigned char>(byte);
    }
  
  void ReadData(void *data, unsigned long bytes)
    {
    if(m_File == NULL)
      {
      ExceptionObject exception;
      exception.SetDescription("File cannot be read");
      throw exception;
      }

    size_t bread = fread(data, 1, bytes, m_File);
    if(static_cast<unsigned long>(bread) != bytes)
      {
      std::ostringstream oss;
      oss << "File size does not match header: " 
        << bytes << " bytes requested but only "
        << bread << " bytes available!" << std::endl
        << "At file position " << ftell(m_File);
      ExceptionObject exception;
      exception.SetDescription(oss.str().c_str());
      throw exception;
      }
    }

  void WriteData(const void *data, unsigned long bytes)
    {
    if(m_File == NULL)
      {
      ExceptionObject exception;
      exception.SetDescription("File cannot be written");
      throw exception;
      }

    size_t bwritten = fwrite(data, 1, bytes, m_File);
    if(static_cast<unsigned long>(bwritten) != bytes)
      {
      ExceptionObject exception;
      exception.SetDescription("Could not write all bytes to file");
      throw exception;
      }
    }
private:
  FILE *m_File;
};


/**
 * A swap helper class, used to perform swapping for any input
 * data type.
 */
template<typename TPixel> class VoxBoCUBImageIOSwapHelper
{
public:
  typedef ImageIOBase::ByteOrder ByteOrder;
  static void SwapIfNecessary(
    void *buffer, unsigned long numberOfBytes, ByteOrder order)
    {
    if ( order == ImageIOBase::LittleEndian )
      {
      ByteSwapper<TPixel>::SwapRangeFromSystemToLittleEndian(
        (TPixel*)buffer, numberOfBytes / sizeof(TPixel) );
      }
    else if ( order == ImageIOBase::BigEndian )
      {
      ByteSwapper<TPixel>::SwapRangeFromSystemToBigEndian(
        (TPixel *)buffer, numberOfBytes / sizeof(TPixel) );
      }
    }
};


// Strings
const char *VoxBoCUBImageIO::VB_IDENTIFIER_SYSTEM = "VB98";
const char *VoxBoCUBImageIO::VB_IDENTIFIER_FILETYPE = "CUB1";
const char *VoxBoCUBImageIO::VB_DIMENSIONS = "VoxDims(XYZ)";
const char *VoxBoCUBImageIO::VB_SPACING = "VoxSizes(XYZ)";
const char *VoxBoCUBImageIO::VB_ORIGIN = "Origin(XYZ)";
const char *VoxBoCUBImageIO::VB_DATATYPE = "DataType";
const char *VoxBoCUBImageIO::VB_BYTEORDER = "Byteorder";
const char *VoxBoCUBImageIO::VB_ORIENTATION = "Orientation";
const char *VoxBoCUBImageIO::VB_BYTEORDER_MSB = "msbfirst";
const char *VoxBoCUBImageIO::VB_BYTEORDER_LSB = "lsbfirst";
const char *VoxBoCUBImageIO::VB_DATATYPE_BYTE = "Byte";
const char *VoxBoCUBImageIO::VB_DATATYPE_INT = "Integer";
const char *VoxBoCUBImageIO::VB_DATATYPE_FLOAT = "Float";
const char *VoxBoCUBImageIO::VB_DATATYPE_DOUBLE = "Double";

/** Constructor */
VoxBoCUBImageIO::VoxBoCUBImageIO()
{
  InitializeOrientationMap();
  m_ByteOrder = BigEndian;
  m_Reader = NULL;
  m_Writer = NULL;
}


/** Destructor */
VoxBoCUBImageIO::~VoxBoCUBImageIO()
{
  if(m_Reader)
    delete m_Reader;
  if(m_Writer)
    delete m_Writer;
}

GenericCUBFileAdaptor *
VoxBoCUBImageIO::CreateReader(const char *filename)
{
  try
    {
    bool compressed;
    if(CheckExtension(filename, compressed))
      if(compressed)
#ifdef SNAP_GZIP_SUPPORT
          return new CompressedCUBFileAdaptor(filename, "rb");
#else
          return NULL;
#endif
      else
        return new DirectCUBFileAdaptor(filename, "rb");
    else
      return NULL;
    }
  catch(...)
    {
    return NULL;
    }
}

GenericCUBFileAdaptor *
VoxBoCUBImageIO::CreateWriter(const char *filename)
{
  try
    {
    bool compressed;
    if(CheckExtension(filename, compressed))
      if(compressed)
#ifdef SNAP_GZIP_SUPPORT
          return new CompressedCUBFileAdaptor(filename, "rb");
#else
          return NULL;
#endif        
      else
        return new DirectCUBFileAdaptor(filename, "wb");
    else
      return NULL;
    }
  catch(...)
    {
    return NULL;
    }
}

bool VoxBoCUBImageIO::CanReadFile( const char* filename ) 
{ 
  // First check if the file can be read
  GenericCUBFileAdaptor *reader = CreateReader(filename);
  if(reader == NULL)
    {
    itkDebugMacro(<<"The file is not a valid CUB file");
    return false;
    }
    
  // Now check the content
  bool iscub = true;
  try 
    {
    // Get the header
    std::istringstream iss(reader->ReadHeader());

    // Read the first two words
    std::string word;

    // Read the first line from the file
    iss >> word;
    if(word != VB_IDENTIFIER_SYSTEM)
      iscub = false;

    // Read the second line
    iss >> word;
    if(word != VB_IDENTIFIER_FILETYPE)
      iscub = false;
    }
  catch(...)
    { 
    iscub = false; 
    }

  delete reader;
  return iscub;
}

bool VoxBoCUBImageIO::CanWriteFile( const char * name )
{
  bool compressed;
  return CheckExtension(name, compressed);
}

void VoxBoCUBImageIO::Read(void* buffer)
{
  if(m_Reader == NULL)
    {
    ExceptionObject exception(__FILE__, __LINE__);
    exception.SetDescription("File cannot be read");
    throw exception;
    }

  m_Reader->ReadData(buffer, GetImageSizeInBytes());
  this->SwapBytesIfNecessary(buffer, GetImageSizeInBytes());
}

/** 
 *  Read Information about the VoxBoCUB file
 *  and put the cursor of the stream just before the first data pixel
 */
void VoxBoCUBImageIO::ReadImageInformation()
{
  // Make sure there is no other reader
  if(m_Reader)
    delete m_Reader;

  // Create a reader
  m_Reader = CreateReader(m_FileName.c_str());
  if(m_Reader == NULL)
    {
    ExceptionObject exception(__FILE__, __LINE__);
    exception.SetDescription("File cannot be read");
    throw exception;
    }

  // Set the number of dimensions to three
  SetNumberOfDimensions(3);

  // Read the file header
  std::istringstream issHeader(m_Reader->ReadHeader());

  // Read every string in the header. Parse the strings that are special
  while(issHeader.good())
    {
    // Read a line from the stream
    char linebuffer[512];
    issHeader.getline(linebuffer, 512);

    // Get the key string
    std::istringstream iss(linebuffer);
    std::string key;

    // Read the key and strip the colon from it
    iss >> key;
    if(key.size() > 0 && key[key.size() - 1] == ':')
      {
      // Strip the colon off the key
      key = key.substr(0, key.size() - 1);

      // Check if this is a relevant key
      if(key == VB_DIMENSIONS)
        {
        iss >> m_Dimensions[0];
        iss >> m_Dimensions[1];
        iss >> m_Dimensions[2];
        }

      else if(key == VB_SPACING)
        {
        iss >> m_Spacing[0];
        iss >> m_Spacing[1];
        iss >> m_Spacing[2];
        }

      else if(key == VB_ORIGIN)
        {
        double ox, oy, oz;
        iss >> ox; iss >> oy; iss >> oz;
        m_Origin[0] = ox * m_Spacing[0];
        m_Origin[1] = oy * m_Spacing[1];
        m_Origin[2] = oz * m_Spacing[2];
        }

      else if(key == VB_DATATYPE)
        {
        std::string type;
        iss >> type;
        m_PixelType = SCALAR;
        if(type == VB_DATATYPE_BYTE)
          m_ComponentType = UCHAR;
        else if(type == VB_DATATYPE_INT)
          m_ComponentType = USHORT;
        else if(type == VB_DATATYPE_FLOAT)
          m_ComponentType = FLOAT;
        else if(type == VB_DATATYPE_DOUBLE)
          m_ComponentType = DOUBLE;
        }

      else if(key == VB_BYTEORDER)
        {
        std::string type;
        iss >> type;
        if(type == VB_BYTEORDER_MSB)
          SetByteOrderToBigEndian();
        else if(type == VB_BYTEORDER_LSB)
          SetByteOrderToLittleEndian();
        else
          {
          ExceptionObject exception(__FILE__, __LINE__);
          exception.SetDescription("Unknown byte order constant");
          throw exception;
          }
        }

      else if(key == VB_ORIENTATION)
        {
        std::string code;
        iss >> code;

        // Set the orientation code in the data dictionary
        OrientationMap::const_iterator it = m_OrientationMap.find(code);
        if(it != m_OrientationMap.end())
          {
              //Octavian original begin
          //itk::MetaDataDictionary &dic =this->GetMetaDataDictionary();
          //EncapsulateMetaData<OrientationFlags>(
            //dic, ITK_CoordinateOrientation, it->second);
              //Octavian original end
          //NOTE:  The itk::ImageIOBase direction is a std::vector<std::vector > >, and threeDDirection is a 3x3 matrix
          itk::SpatialOrientationAdapter soAdaptor;
          itk::SpatialOrientationAdapter::DirectionType threeDDirection=soAdaptor.ToDirectionCosines(it->second);
          this->m_Direction[0][0]=threeDDirection[0][0];
          this->m_Direction[0][1]=threeDDirection[0][1];
          this->m_Direction[0][2]=threeDDirection[0][2];
          this->m_Direction[1][0]=threeDDirection[1][0];
          this->m_Direction[1][1]=threeDDirection[1][1];
          this->m_Direction[1][2]=threeDDirection[1][2];
          this->m_Direction[2][0]=threeDDirection[2][0];
          this->m_Direction[2][1]=threeDDirection[2][1];
          this->m_Direction[2][2]=threeDDirection[2][2];
          }
        }

      else
        {
        // Encode the right hand side of the string in the meta-data dic
        std::string word;
        std::ostringstream oss;
        while(iss >> word)
          {
          if(oss.str().size())
            oss << " ";
          oss << word;
          }
        itk::MetaDataDictionary &dic =this->GetMetaDataDictionary();
        EncapsulateMetaData<std::string>(dic, key, oss.str());
        }
      }
    }
}

void 
VoxBoCUBImageIO
::WriteImageInformation(void)
{
  // See if we have a writer already
  if(m_Writer != NULL)
    delete m_Writer;

  // First check if the file can be written to
  m_Writer = CreateWriter(m_FileName.c_str());
  if(m_Writer == NULL)
    {
    ExceptionObject exception(__FILE__, __LINE__);
    exception.SetDescription("File cannot be read");
    throw exception;
    }

  // Check that the number of dimensions is correct
  if(GetNumberOfDimensions() != 3)
    {
    ExceptionObject exception(__FILE__, __LINE__);
    exception.SetDescription("Unsupported number of dimensions");
    throw exception;
    }

  // Put together a header
  std::ostringstream header;

  // Write the identifiers
  header << VB_IDENTIFIER_SYSTEM << std::endl;
  header << VB_IDENTIFIER_FILETYPE << std::endl;

  // Write the image dimensions
  header << VB_DIMENSIONS << ": " 
    << m_Dimensions[0] << " "
    << m_Dimensions[1] << " "
    << m_Dimensions[2] << std::endl;

  // Write the spacing
  header << VB_SPACING << ": "
    << m_Spacing[0] << " "
    << m_Spacing[1] << " "
    << m_Spacing[2] << std::endl;

  // Write the origin (have to convert to bytes)
  header << VB_ORIGIN << ": "
    << static_cast< int >( m_Origin[0] / m_Spacing[0] + 0.5 ) << " "
    << static_cast< int >( m_Origin[1] / m_Spacing[1] + 0.5 ) << " "
    << static_cast< int >( m_Origin[2] / m_Spacing[2] + 0.5 ) << std::endl;

  // Write the byte order
  header << VB_BYTEORDER << ": "
    << (ByteSwapper<char>::SystemIsBigEndian() 
      ? VB_BYTEORDER_MSB : VB_BYTEORDER_LSB) << std::endl;

  // Write the data type 
  switch(m_ComponentType) 
    {
    case CHAR: 
    case UCHAR:
      header << VB_DATATYPE << ": " << VB_DATATYPE_BYTE << std::endl;
      break;
    case SHORT:
    case USHORT:
      header << VB_DATATYPE << ": " << VB_DATATYPE_INT << std::endl;
      break;
    case FLOAT:
      header << VB_DATATYPE << ": " << VB_DATATYPE_FLOAT << std::endl;
      break;
    case DOUBLE:
      header << VB_DATATYPE << ": " << VB_DATATYPE_DOUBLE << std::endl;
      break;
    default:
      ExceptionObject exception(__FILE__, __LINE__);
      exception.SetDescription("Unsupported pixel component type");
      throw exception;
    }

  // Write the orientation code
    //Octavian original begin
  //MetaDataDictionary &dic = GetMetaDataDictionary();
  //OrientationFlags oflag = SpatialOrientation::ITK_COORDINATE_ORIENTATION_INVALID;
  //if(ExposeMetaData<OrientationFlags>(dic, ITK_CoordinateOrientation, oflag))
      //Octavian original end
    //NOTE:  The itk::ImageIOBase direction is a std::vector<std::vector > >, and threeDDirection is a 3x3 matrix
    itk::SpatialOrientationAdapter soAdaptor;
    itk::SpatialOrientationAdapter::DirectionType threeDDirection;
    threeDDirection[0][0]=this->m_Direction[0][0];
    threeDDirection[0][1]=this->m_Direction[0][1];
    threeDDirection[0][2]=this->m_Direction[0][2];
    threeDDirection[1][0]=this->m_Direction[1][0];
    threeDDirection[1][1]=this->m_Direction[1][1];
    threeDDirection[1][2]=this->m_Direction[1][2];
    threeDDirection[2][0]=this->m_Direction[2][0];
    threeDDirection[2][1]=this->m_Direction[2][1];
    threeDDirection[2][2]=this->m_Direction[2][2];
    OrientationFlags     oflag = soAdaptor.FromDirectionCosines(threeDDirection);
    {
    InverseOrientationMap::const_iterator it = 
      m_InverseOrientationMap.find(oflag);
    if(it != m_InverseOrientationMap.end())
      {
      header << VB_ORIENTATION << ": " << it->second << std::endl;
      }
    }

  // Write the terminating characters
  header << "\f\n";

  // Write the header to the file as data
  m_Writer->WriteData(header.str().c_str(), header.str().size());
}

/** The write function is not implemented */
void 
VoxBoCUBImageIO
::Write( const void* buffer) 
{
  WriteImageInformation();
  m_Writer->WriteData(buffer, GetImageSizeInBytes());
}

/** Print Self Method */
void VoxBoCUBImageIO::PrintSelf(std::ostream& os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);
  os << indent << "PixelType " << m_PixelType << "\n";
}


bool VoxBoCUBImageIO::CheckExtension(const char* filename, bool &isCompressed)
{
  std::string fname = filename;
  if ( fname == "" )
  {
    itkDebugMacro(<< "No filename specified.");
    return false;
  }

  bool extensionFound = false;
  isCompressed = false;

  std::string::size_type giplPos = fname.rfind(".cub");
  if ((giplPos != std::string::npos)
      && (giplPos == fname.length() - 4))
    {
      extensionFound = true;
    }

  giplPos = fname.rfind(".cub.gz");
  if ((giplPos != std::string::npos)
      && (giplPos == fname.length() - 7))
    {
    extensionFound = true;
    isCompressed = true;
    }

  return extensionFound;
}

void 
VoxBoCUBImageIO
::InitializeOrientationMap()
{
  m_OrientationMap["RIP"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_RIP;
  m_OrientationMap["LIP"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_LIP;
  m_OrientationMap["RSP"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_RSP;
  m_OrientationMap["LSP"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_LSP;
  m_OrientationMap["RIA"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_RIA;
  m_OrientationMap["LIA"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_LIA;
  m_OrientationMap["RSA"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_RSA;
  m_OrientationMap["LSA"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_LSA;
  m_OrientationMap["IRP"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_IRP;
  m_OrientationMap["ILP"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_ILP;
  m_OrientationMap["SRP"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_SRP;
  m_OrientationMap["SLP"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_SLP;
  m_OrientationMap["IRA"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_IRA;
  m_OrientationMap["ILA"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_ILA;
  m_OrientationMap["SRA"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_SRA;
  m_OrientationMap["SLA"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_SLA;
  m_OrientationMap["RPI"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_RPI;
  m_OrientationMap["LPI"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_LPI;
  m_OrientationMap["RAI"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_RAI;
  m_OrientationMap["LAI"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_LAI;
  m_OrientationMap["RPS"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_RPS;
  m_OrientationMap["LPS"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_LPS;
  m_OrientationMap["RAS"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_RAS;
  m_OrientationMap["LAS"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_LAS;
  m_OrientationMap["PRI"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_PRI;
  m_OrientationMap["PLI"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_PLI;
  m_OrientationMap["ARI"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_ARI;
  m_OrientationMap["ALI"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_ALI;
  m_OrientationMap["PRS"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_PRS;
  m_OrientationMap["PLS"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_PLS;
  m_OrientationMap["ARS"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_ARS;
  m_OrientationMap["ALS"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_ALS;
  m_OrientationMap["IPR"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_IPR;
  m_OrientationMap["SPR"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_SPR;
  m_OrientationMap["IAR"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_IAR;
  m_OrientationMap["SAR"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_SAR;
  m_OrientationMap["IPL"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_IPL;
  m_OrientationMap["SPL"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_SPL;
  m_OrientationMap["IAL"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_IAL;
  m_OrientationMap["SAL"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_SAL;
  m_OrientationMap["PIR"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_PIR;
  m_OrientationMap["PSR"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_PSR;
  m_OrientationMap["AIR"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_AIR;
  m_OrientationMap["ASR"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_ASR;
  m_OrientationMap["PIL"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_PIL;
  m_OrientationMap["PSL"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_PSL;
  m_OrientationMap["AIL"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_AIL;
  m_OrientationMap["ASL"] = SpatialOrientation::ITK_COORDINATE_ORIENTATION_ASL;

  OrientationMap::const_iterator it;
  for(it = m_OrientationMap.begin(); it != m_OrientationMap.end(); ++it)
    m_InverseOrientationMap[it->second] = it->first;

}

void 
VoxBoCUBImageIO
::SwapBytesIfNecessary(void *buffer, unsigned long numberOfBytes)
{
  if(m_ComponentType == CHAR)
    VoxBoCUBImageIOSwapHelper<char>::SwapIfNecessary(
      buffer, numberOfBytes, m_ByteOrder);
  else if(m_ComponentType == UCHAR)
    VoxBoCUBImageIOSwapHelper<unsigned char>::SwapIfNecessary(
      buffer, numberOfBytes, m_ByteOrder);
  else if(m_ComponentType == SHORT)
    VoxBoCUBImageIOSwapHelper<short>::SwapIfNecessary(
      buffer, numberOfBytes, m_ByteOrder);
  else if(m_ComponentType == USHORT)
    VoxBoCUBImageIOSwapHelper<unsigned short>::SwapIfNecessary(
      buffer, numberOfBytes, m_ByteOrder);
  else if(m_ComponentType == INT)
    VoxBoCUBImageIOSwapHelper<int>::SwapIfNecessary(
      buffer, numberOfBytes, m_ByteOrder);
  else if(m_ComponentType == UINT)
    VoxBoCUBImageIOSwapHelper<unsigned int>::SwapIfNecessary(
      buffer, numberOfBytes, m_ByteOrder);
  else if(m_ComponentType == LONG)
    VoxBoCUBImageIOSwapHelper<long>::SwapIfNecessary(
      buffer, numberOfBytes, m_ByteOrder);
  else if(m_ComponentType == ULONG)
    VoxBoCUBImageIOSwapHelper<unsigned long>::SwapIfNecessary(
      buffer, numberOfBytes, m_ByteOrder);
  else if(m_ComponentType == FLOAT)
    VoxBoCUBImageIOSwapHelper<float>::SwapIfNecessary(
      buffer, numberOfBytes, m_ByteOrder);
  else if(m_ComponentType == DOUBLE)
    VoxBoCUBImageIOSwapHelper<double>::SwapIfNecessary(
      buffer, numberOfBytes, m_ByteOrder);
  else 
    {
    ExceptionObject exception(__FILE__, __LINE__);
    exception.SetDescription("Pixel Type Unknown");
    throw exception;
    }
}

} // end namespace itk