File: itkNrrdImageIO.cxx

package info (click to toggle)
insighttoolkit 3.6.0-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 94,956 kB
  • ctags: 74,981
  • sloc: cpp: 355,621; ansic: 195,070; fortran: 28,713; python: 3,802; tcl: 1,996; sh: 1,175; java: 583; makefile: 415; csh: 184; perl: 175
file content (1137 lines) | stat: -rwxr-xr-x 36,331 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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkNrrdImageIO.cxx,v $
  Language:  C++
  Date:      $Date: 2007-05-16 19:44:55 $
  Version:   $Revision: 1.38 $

  Copyright (c) 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.

=========================================================================*/
#ifdef _MSC_VER
#pragma warning ( disable : 4786 )
#endif

#include <string>
#include "itkNrrdImageIO.h"
#include "itkMacro.h"
#include "itkMetaDataObject.h"
#include "itkIOCommon.h"

#if defined(__BORLANDC__) 
# include <math.h> 
# include <float.h> // for _control87() 
#endif // defined(__BORLANDC__) 

namespace itk {

#define KEY_PREFIX "NRRD_"
 
bool NrrdImageIO::SupportsDimension(unsigned long dim)
{
  if (1 == this->GetNumberOfComponents())
    {
    return dim <= NRRD_DIM_MAX;
    }
  else
    {
    return dim <= NRRD_DIM_MAX - 1;
    }
}

void NrrdImageIO::PrintSelf(std::ostream& os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);
}
 
ImageIOBase::IOComponentType
NrrdImageIO::
NrrdToITKComponentType( const int nrrdComponentType ) const
{
#if defined(__BORLANDC__) 
// Disable floating point exceptions in Borland 
  _control87(MCW_EM, MCW_EM); 
#endif // defined(__BORLANDC__) 
  switch( nrrdComponentType )
    {
    case nrrdTypeUnknown:
    case nrrdTypeBlock:
      return UNKNOWNCOMPONENTTYPE;
      break;
    case nrrdTypeChar:
      return CHAR;
      break;
    case nrrdTypeUChar:
      return UCHAR;
      break;
    case nrrdTypeShort:
      return SHORT;
      break;
    case nrrdTypeUShort:
      return USHORT;
      break;
    // "long" is a silly type because it basically guaranteed not to be
    // cross-platform across 32-vs-64 bit machines, but we'll use it 
    // where possible.
    case nrrdTypeLLong:
      return airMy32Bit ? UNKNOWNCOMPONENTTYPE : LONG;
      break;
    case nrrdTypeULLong:
      return airMy32Bit ? UNKNOWNCOMPONENTTYPE : ULONG;
      break;
    case nrrdTypeInt:
      return INT;
      break;
    case nrrdTypeUInt:
      return UINT;
      break;
    case nrrdTypeFloat:
      return FLOAT;
      break;
    case nrrdTypeDouble:
      return DOUBLE;
      break;
    default:
      return UNKNOWNCOMPONENTTYPE;
      break;
    }

  // Strictly to avoid compiler warning regarding "control may reach end of
  // non-void function":
  //
  return UNKNOWNCOMPONENTTYPE;
}

int
NrrdImageIO::
ITKToNrrdComponentType( const ImageIOBase::IOComponentType itkComponentType ) const
{
#if defined(__BORLANDC__) 
// Disable floating point exceptions in Borland 
  _control87(MCW_EM, MCW_EM); 
#endif // defined(__BORLANDC__) 
  switch( itkComponentType )
    {
    case UNKNOWNCOMPONENTTYPE:
      return nrrdTypeUnknown;
      break;
    case CHAR:
      return nrrdTypeChar;
      break;
    case UCHAR:
      return nrrdTypeUChar;
      break;
    case SHORT:
      return nrrdTypeShort;
      break;
    case USHORT:
      return nrrdTypeUShort;
      break;
    // "long" is a silly type because it basically guaranteed not to be
    // cross-platform across 32-vs-64 bit machines, but we can figure out
    // a cross-platform way of storing the information.
    case LONG:
      return airMy32Bit ? nrrdTypeInt : nrrdTypeLLong;
      break;
    case ULONG:
      return airMy32Bit ? nrrdTypeUInt : nrrdTypeULLong;
      break;
    case INT:
      return nrrdTypeInt;
      break;
    case UINT:
      return nrrdTypeUInt;
      break;
    case FLOAT:
      return nrrdTypeFloat;
      break;
    case DOUBLE:
      return nrrdTypeDouble;
      break;
    default:
      return nrrdTypeUnknown;
      break;
    }

  // Strictly to avoid compiler warning regarding "control may reach end of
  // non-void function":
  //
  return nrrdTypeUnknown;
}

bool NrrdImageIO::CanReadFile( const char* filename ) 
{
#if defined(__BORLANDC__) 
// Disable floating point exceptions in Borland 
  _control87(MCW_EM, MCW_EM); 
#endif // defined(__BORLANDC__) 
  // Check the extension first to avoid opening files that do not
  // look like nrrds.  The file must have an appropriate extension to be
  // recognized.
  std::string fname = filename;
  if(  fname == "" )
    {
    itkDebugMacro(<<"No filename specified.");
    return false;
    }

  bool extensionFound = false;
  std::string::size_type nrrdPos = fname.rfind(".nrrd");
  if ((nrrdPos != std::string::npos)
      && (nrrdPos == fname.length() - 5))
    {
    extensionFound = true;
    }

  std::string::size_type nhdrPos = fname.rfind(".nhdr");
  if ((nhdrPos != std::string::npos)
      && (nhdrPos == fname.length() - 5))
    {
    extensionFound = true;
    }

  if( !extensionFound )
    {
    itkDebugMacro(<<"The filename extension is not recognized");
    return false;
    }

  // We have the correct extension, so now check for the Nrrd magic "NRRD",
  // while ignoring the format version (the next four characters)
  std::ifstream inputStream;

  inputStream.open( filename, std::ios::in | std::ios::binary );

  if( inputStream.fail() )
    {
    return false;
    }

  char magic[5] = {'\0','\0','\0','\0','\0'};
  inputStream.read(magic,4*sizeof(char));

  if( inputStream.eof() )
    {
    inputStream.close();
    return false;
    }

  if( strcmp(magic,"NRRD") == 0 ) 
    {
    inputStream.close();
    return true;
    }

  inputStream.close();
  return false;
}

void NrrdImageIO::ReadImageInformation()
{
#if defined(__BORLANDC__) 
// Disable floating point exceptions in Borland 
  _control87(MCW_EM, MCW_EM); 
#endif // defined(__BORLANDC__) 
  // This method determines the following and sets the appropriate value in
  // the parent IO class:
  //
  // binary/ascii file type 
  // endianness
  // pixel type
  // pixel component type
  // number of pixel components
  // number of image dimensions
  // image spacing
  // image origin
  // meta data dictionary information
  
  Nrrd *nrrd = nrrdNew();
  NrrdIoState *nio = nrrdIoStateNew();
  
  // this is the mechanism by which we tell nrrdLoad to read
  // just the header, and none of the data
  nrrdIoStateSet(nio, nrrdIoStateSkipData, 1);
  if (nrrdLoad(nrrd, this->GetFileName(), nio) != 0)
    {
    char *err = biffGetDone(NRRD);  // would be nice to free(err)
    itkExceptionMacro("ReadImageInformation: Error reading " 
                      << this->GetFileName() << ":\n" << err);
    }

  if (nrrdTypeBlock == nrrd->type)
    {
    itkExceptionMacro("ReadImageInformation: Cannot currently "
                      "handle nrrdTypeBlock");
    }
  if ( nio->endian == airEndianLittle )
    {
    this->SetByteOrderToLittleEndian();
    }
  else if (nio->endian == airEndianBig )
    {
    this->SetByteOrderToBigEndian();
    }
  else
    {
    this->SetByteOrder( ImageIOBase::OrderNotApplicable );
    }

  if ( nio->encoding == nrrdEncodingAscii )
    {
    this->SetFileTypeToASCII();
    }
  else
    {
    this->SetFileTypeToBinary();
    }
  // set type of pixel components; this is orthogonal to pixel type

  ImageIOBase::IOComponentType
    cmpType = this->NrrdToITKComponentType(nrrd->type);
  if (UNKNOWNCOMPONENTTYPE == cmpType)
    {
    itkExceptionMacro("Nrrd type " << airEnumStr(nrrdType, nrrd->type)
                      << " could not be mapped to an ITK component type");
    }
  this->SetComponentType( cmpType );

  // Set the number of image dimensions and bail if needed
  unsigned int domainAxisNum, domainAxisIdx[NRRD_DIM_MAX],
    rangeAxisNum, rangeAxisIdx[NRRD_DIM_MAX];
  domainAxisNum = nrrdDomainAxesGet(nrrd, domainAxisIdx);
  rangeAxisNum = nrrdRangeAxesGet(nrrd, rangeAxisIdx);
  if (nrrd->spaceDim && nrrd->spaceDim != domainAxisNum)
    {
    itkExceptionMacro("ReadImageInformation: nrrd's # independent axes ("
                      << domainAxisNum << ") doesn't match dimension of space"
                      " in which orientation is defined ("
                      << nrrd->spaceDim << "); not currently handled");
    }
  // else nrrd->spaceDim == domainAxisNum when nrrd has orientation
  
  if (0 == rangeAxisNum)
    {
    // we don't have any non-scalar data
    this->SetNumberOfDimensions(nrrd->dim);
    this->SetPixelType( ImageIOBase::SCALAR );
    this->SetNumberOfComponents(1);
    }
  else if (1 == rangeAxisNum)
    {
    this->SetNumberOfDimensions(nrrd->dim - 1);
    unsigned int kind = nrrd->axis[rangeAxisIdx[0]].kind;
    unsigned int size = nrrd->axis[rangeAxisIdx[0]].size;
    // NOTE: it is the NRRD readers responsibility to make sure that
    // the size (# of components) associated with a specific kind is
    // matches the actual size of the axis.
    switch(kind)
      {
      case nrrdKindDomain:
      case nrrdKindSpace:
      case nrrdKindTime:
        itkExceptionMacro("ReadImageInformation: range axis kind ("
                          << airEnumStr(nrrdKind, kind) << ") seems more "
                          "like a domain axis than a range axis");
        break;
      case nrrdKindStub:
      case nrrdKindScalar:
        this->SetPixelType( ImageIOBase::SCALAR );
        this->SetNumberOfComponents(size);
        break;
      case nrrdKind3Color:
      case nrrdKindRGBColor:
        this->SetPixelType( ImageIOBase::RGB );
        this->SetNumberOfComponents(size);
        break;
      case nrrdKind4Color:
      case nrrdKindRGBAColor:
        this->SetPixelType( ImageIOBase::RGBA );
        this->SetNumberOfComponents(size);
        break;
      case nrrdKindVector:
      case nrrdKind2Vector:
      case nrrdKind3Vector:
      case nrrdKind4Vector:
      case nrrdKindList:
        this->SetPixelType( ImageIOBase::VECTOR );
        this->SetNumberOfComponents(size);
        break;
      case nrrdKindPoint:
        this->SetPixelType( ImageIOBase::POINT );
        this->SetNumberOfComponents(size);
        break;
      case nrrdKindCovariantVector:
      case nrrdKind3Gradient:
      case nrrdKindNormal:
      case nrrdKind3Normal:
        this->SetPixelType( ImageIOBase::COVARIANTVECTOR );
        this->SetNumberOfComponents(size);
        break;
      case nrrdKind3DSymMatrix:
        // ImageIOBase::DIFFUSIONTENSOR3D is a subclass
        this->SetPixelType( ImageIOBase::SYMMETRICSECONDRANKTENSOR );
        this->SetNumberOfComponents(size);
        break;
      case nrrdKind3DMaskedSymMatrix:
        this->SetPixelType( ImageIOBase::SYMMETRICSECONDRANKTENSOR );
        // NOTE: we will crop out the mask in Read() below; this is the
        // one case where NumberOfComponents != size
        this->SetNumberOfComponents(size-1);
        break;
      case nrrdKindComplex:
        this->SetPixelType( ImageIOBase::COMPLEX );
        this->SetNumberOfComponents(size);
        break;
      case nrrdKindHSVColor:
      case nrrdKindXYZColor:
      case nrrdKindQuaternion:
      case nrrdKind2DSymMatrix:
      case nrrdKind2DMaskedSymMatrix:
      case nrrdKind2DMatrix:
      case nrrdKind2DMaskedMatrix:
      case nrrdKind3DMatrix:
        // for all other Nrrd kinds, we punt and call it a vector
        this->SetPixelType( ImageIOBase::VECTOR );
        this->SetNumberOfComponents(size);
        break;
      default:
        itkExceptionMacro("ReadImageInformation: nrrdKind " << kind 
                          << " not known!");
        break;
      }
    }
  else 
    {
    itkExceptionMacro("ReadImageInformation: nrrd has "
                      << rangeAxisNum 
                      << " dependent axis (not 1); not currently handled");
    }
  
  double spacing;
  double spaceDir[NRRD_SPACE_DIM_MAX];
  std::vector<double> spaceDirStd(domainAxisNum);
  int spacingStatus;
  
  int iFlipFactors[3];  // used to flip the measurement frame later on
  for (unsigned int iI=0; iI<3; iI++ )
    {
    iFlipFactors[iI] = 1;
    }

  for (unsigned int axii=0; axii < domainAxisNum; axii++)
    {
    unsigned int naxi = domainAxisIdx[axii];
    this->SetDimensions(axii, nrrd->axis[naxi].size);
    spacingStatus = nrrdSpacingCalculate(nrrd, naxi, &spacing, spaceDir);

    switch(spacingStatus) 
      {
      case nrrdSpacingStatusNone:
        // Let ITK's defaults stay
        // this->SetSpacing(axii, 1.0);
        break;
      case nrrdSpacingStatusScalarNoSpace:
        this->SetSpacing(axii, spacing);
        break;
      case nrrdSpacingStatusDirection:
        if (AIR_EXISTS(spacing))
          {
          // only set info if we have something to set
          switch (nrrd->space)
            {
            // on read, convert non-LPS coords into LPS coords, when we can
            case nrrdSpaceRightAnteriorSuperior:
              spaceDir[0] *= -1;   // R -> L
              spaceDir[1] *= -1;   // A -> P
              iFlipFactors[0] = -1;
              iFlipFactors[1] = -1;
              break;
            case nrrdSpaceLeftAnteriorSuperior:
              spaceDir[0] *= -1;   // R -> L
              iFlipFactors[0] = -1;
              break;
            case nrrdSpaceLeftPosteriorSuperior:
              // no change needed
              break;
            default:
              // we're not coming from a space for which the conversion
              // to LPS is well-defined
              break;
            }
          this->SetSpacing(axii, spacing);

          for (unsigned int saxi=0; saxi < nrrd->spaceDim; saxi++)
            {
            spaceDirStd[saxi] = spaceDir[saxi];
            }
          this->SetDirection(axii, spaceDirStd);
          }
        break;
      default:
      case nrrdSpacingStatusUnknown:
        itkExceptionMacro("ReadImageInformation: Error interpreting "
                          "nrrd spacing (nrrdSpacingStatusUnknown)");
        break;
      case nrrdSpacingStatusScalarWithSpace:
        itkExceptionMacro("ReadImageInformation: Error interpreting "
                          "nrrd spacing (nrrdSpacingStatusScalarWithSpace)");
        break;
      }
    }
  
  // Figure out origin
  if (nrrd->spaceDim)
    {
    if (AIR_EXISTS(nrrd->spaceOrigin[0]))
      {
      // only set info if we have something to set
      double spaceOrigin[NRRD_SPACE_DIM_MAX];
      for (unsigned int saxi=0; saxi < nrrd->spaceDim; saxi++)
        {
        spaceOrigin[saxi] = nrrd->spaceOrigin[saxi];
        }
      switch (nrrd->space)
        {
        // convert non-LPS coords into LPS coords, when we can
        case nrrdSpaceRightAnteriorSuperior:
          spaceOrigin[0] *= -1;   // R -> L
          spaceOrigin[1] *= -1;   // A -> P
          break;
        case nrrdSpaceLeftAnteriorSuperior:
          spaceOrigin[0] *= -1;   // R -> L
          break;
        case nrrdSpaceLeftPosteriorSuperior:
          // no change needed
          break;
        default:
          // we're not coming from a space for which the conversion
          // to LPS is well-defined
          break;
        }
      for (unsigned int saxi=0; saxi < nrrd->spaceDim; saxi++)
        {
        this->SetOrigin(saxi, spaceOrigin[saxi]);
        }
      }
    }
  else 
    {
    double spaceOrigin[NRRD_DIM_MAX];
    int originStatus = nrrdOriginCalculate(nrrd, domainAxisIdx, domainAxisNum,
                                           nrrdCenterCell, spaceOrigin);
    for (unsigned int saxi=0; saxi < domainAxisNum; saxi++) 
      {
      switch (originStatus)
        {
        case nrrdOriginStatusNoMin:
        case nrrdOriginStatusNoMaxOrSpacing:
          // only set info if we have something to set
          // this->SetOrigin(saxi, 0.0);
          break;
        case nrrdOriginStatusOkay:
          this->SetOrigin(saxi, spaceOrigin[saxi]);
          break;
        default:
        case nrrdOriginStatusUnknown:
        case nrrdOriginStatusDirection:
          itkExceptionMacro("ReadImageInformation: Error interpreting "
                            "nrrd origin status");
          break;
        }
      }
    }

  // Store key/value pairs in MetaDataDictionary
  char key[AIR_STRLEN_SMALL];
  const char *val;
  char *keyPtr = NULL;
  char *valPtr = NULL;
  MetaDataDictionary &thisDic=this->GetMetaDataDictionary();
  std::string classname(this->GetNameOfClass());
  EncapsulateMetaData<std::string>(thisDic, ITK_InputFilterName, classname);
  for (unsigned int kvpi=0; kvpi < nrrdKeyValueSize(nrrd); kvpi++)
    {
    nrrdKeyValueIndex(nrrd, &keyPtr, &valPtr, kvpi);
    EncapsulateMetaData<std::string>(thisDic, std::string(keyPtr), 
                                     std::string(valPtr));
    keyPtr = (char *)airFree(keyPtr);
    valPtr = (char *)airFree(valPtr);
    }
  
  // save in MetaDataDictionary those important nrrd fields that
  // (currently) have no ITK equivalent. NOTE that for the per-axis
  // information, we use the same axis index (axii) as in ITK, NOT
  // the original axis index in nrrd (axi).  This is because in the
  // Read() method, non-scalar data is permuted to the fastest axis,
  // on the on the Write() side, its always written to the fastest axis,
  // so we might was well go with consistent and idiomatic indexing.
  NrrdAxisInfo *naxis;
  for (unsigned int axii=0; axii < domainAxisNum; axii++)
    {
    unsigned int axi = domainAxisIdx[axii];
    naxis = nrrd->axis + axi;
    if (AIR_EXISTS(naxis->thickness))
      {
      sprintf(key, "%s%s[%d]", KEY_PREFIX,
              airEnumStr(nrrdField, nrrdField_thicknesses), axii);
      EncapsulateMetaData<double>(thisDic, std::string(key),
                                  naxis->thickness);
      }
    if (naxis->center)
      {
      sprintf(key, "%s%s[%d]", KEY_PREFIX, 
              airEnumStr(nrrdField, nrrdField_centers), axii);
      val = airEnumStr(nrrdCenter, naxis->center);
      EncapsulateMetaData<std::string>(thisDic, std::string(key),
                                       std::string(val));
      }
    if (naxis->kind)
      {
      sprintf(key, "%s%s[%d]", KEY_PREFIX, 
              airEnumStr(nrrdField, nrrdField_kinds), axii);
      val = airEnumStr(nrrdKind, naxis->kind);
      EncapsulateMetaData<std::string>(thisDic, std::string(key),
                                       std::string(val));
      }
    if (airStrlen(naxis->label))
      {
      sprintf(key, "%s%s[%d]", KEY_PREFIX, 
              airEnumStr(nrrdField, nrrdField_labels), axii);
      EncapsulateMetaData<std::string>(thisDic, std::string(key),
                                       std::string(naxis->label));
      }
    }
  if (airStrlen(nrrd->content))
    {
    sprintf(key, "%s%s", KEY_PREFIX, 
            airEnumStr(nrrdField, nrrdField_content));
    EncapsulateMetaData<std::string>(thisDic, std::string(key), 
                                     std::string(nrrd->content));
    }
  if (AIR_EXISTS(nrrd->oldMin))
    {
    sprintf(key, "%s%s", KEY_PREFIX,
            airEnumStr(nrrdField, nrrdField_old_min));
    EncapsulateMetaData<double>(thisDic, std::string(key), nrrd->oldMin);
    }
  if (AIR_EXISTS(nrrd->oldMax))
    {
    sprintf(key, "%s%s", KEY_PREFIX,
            airEnumStr(nrrdField, nrrdField_old_max));
    EncapsulateMetaData<double>(thisDic, std::string(key), nrrd->oldMax);
    }
  if (nrrd->space)
    {
    sprintf(key, "%s%s", KEY_PREFIX,
            airEnumStr(nrrdField, nrrdField_space));
    val = airEnumStr(nrrdSpace, nrrd->space);

    // keep everything consistent: so enter it as LPS in the meta data
    // dictionary in case it could get converted, otherwise leave it
    // as is

    switch (nrrd->space) 
      {
      case nrrdSpaceRightAnteriorSuperior:
      case nrrdSpaceLeftAnteriorSuperior:
      case nrrdSpaceLeftPosteriorSuperior:
        // in all these cases we could convert
        EncapsulateMetaData<std::string>(thisDic, std::string(key),
                                         std::string( airEnumStr(nrrdSpace, nrrdSpaceLeftPosteriorSuperior )));
        break;
      default:
        // we're not coming from a space for which the conversion
        // to LPS is well-defined
        EncapsulateMetaData<std::string>(thisDic, std::string(key),
                                         std::string(val));
        break;
      }
    }

  if (AIR_EXISTS(nrrd->measurementFrame[0][0]))
    {
    sprintf(key, "%s%s", KEY_PREFIX,
            airEnumStr(nrrdField, nrrdField_measurement_frame));
    std::vector<std::vector<double> > msrFrame(domainAxisNum);

    // flip the measurement frame here if we have to 
    // so that everything is consistent with the ITK LPS space directions
    // but only do this if we have a three dimensional space or smaller

    for (unsigned int saxi=0; saxi < domainAxisNum; saxi++) 
      {
      msrFrame[saxi].resize(domainAxisNum);
      for (unsigned int saxj=0; saxj < domainAxisNum; saxj++)
        {
        if ( domainAxisNum<=3 ) 
          {
          msrFrame[saxi][saxj] = iFlipFactors[saxj]*nrrd->measurementFrame[saxi][saxj];
          }
        else
          {
          msrFrame[saxi][saxj] = nrrd->measurementFrame[saxi][saxj];
          }
        }
      }
    EncapsulateMetaData<std::vector<std::vector<double> > >(thisDic,
                                                            std::string(key),
                                                            msrFrame);
    }
  
  nrrd = nrrdNix(nrrd);
  nio = nrrdIoStateNix(nio);
}


void NrrdImageIO::Read(void* buffer)
{
#if defined(__BORLANDC__) 
// Disable floating point exceptions in Borland 
  _control87(MCW_EM, MCW_EM); 
#endif // defined(__BORLANDC__) 

  Nrrd *nrrd = nrrdNew();
  unsigned int baseDim;
  bool nrrdAllocated;

  // NOTE the main reason the logic becomes complicated here is that
  // ITK has to be the one to allocate the data segment ("buffer")

  if (ImageIOBase::SYMMETRICSECONDRANKTENSOR == this->GetPixelType())
    {
    // It may be that this is coming from a nrrdKind3DMaskedSymMatrix,
    // in which case ITK's buffer has not been allocated for the
    // actual size of the data.  The data will be allocated by nrrdLoad.
    nrrdAllocated = true;
    }
  else 
    {
    // The data buffer has already been allocated for the correct size.
    // Hand the buffer off to the nrrd, setting just enough info so that
    // the nrrd knows the allocated data size (the axes may actually be out
    // of order in the case of non-scalar data.  Internal to nrrdLoad(), the
    // given buffer will be re-used, instead of allocating new data.
    nrrdAllocated = false;
    nrrd->data = buffer;
    nrrd->type = this->ITKToNrrdComponentType( this->m_ComponentType );
    if ( ImageIOBase::SCALAR == this->m_PixelType )
      {
      baseDim = 0;
      }
    else 
      {
      baseDim = 1;
      nrrd->axis[0].size = this->GetNumberOfComponents();
      }
    nrrd->dim = baseDim + this->GetNumberOfDimensions();
    for (unsigned int axi = 0; axi < this->GetNumberOfDimensions(); axi++)
      {
      nrrd->axis[axi+baseDim].size = this->GetDimensions(axi);
      }
    }

  // Read in the nrrd.  Yes, this means that the header is being read
  // twice: once by NrrdImageIO::ReadImageInformation, and once here
  if ( nrrdLoad(nrrd, this->GetFileName(), NULL) != 0 )
    {
    char *err =  biffGetDone(NRRD); // would be nice to free(err)
    itkExceptionMacro("Read: Error reading " 
                      << this->GetFileName() << ":\n" << err);
    }

  unsigned int rangeAxisNum, rangeAxisIdx[NRRD_DIM_MAX];
  rangeAxisNum = nrrdRangeAxesGet(nrrd, rangeAxisIdx);

  if ( rangeAxisNum > 1)
    {
    itkExceptionMacro("Read: handling more than one non-scalar axis "
                      "not currently handled");
    }
  if (1 == rangeAxisNum && 0 != rangeAxisIdx[0])
    {
    // the range (dependent variable) is not on the fastest axis,
    // so we have to permute axes to put it there, since that is
    // how we set things up in ReadImageInformation() above
    Nrrd *ntmp = nrrdNew();
    unsigned int axmap[NRRD_DIM_MAX];
    axmap[0] = rangeAxisIdx[0];
    for (unsigned int axi=1; axi<nrrd->dim; axi++)
      {
      axmap[axi] = axi - (axi <= rangeAxisIdx[0]);
      }
    // The memory size of the input and output of nrrdAxesPermute is
    // the same; the existing nrrd->data is re-used.
    if (nrrdCopy(ntmp, nrrd)
        || nrrdAxesPermute(nrrd, ntmp, axmap))
      {
      char *err =  biffGetDone(NRRD); // would be nice to free(err)
      itkExceptionMacro("Read: Error permuting independent axis in " 
                        << this->GetFileName() << ":\n" << err);
      }
    nrrdNuke(ntmp);
    }

  if (nrrdAllocated)
    {
    // Now we have to get the data back into the given ITK buffer
    // In any case, the logic here has the luxury of assuming that the
    // *single* non-scalar axis is the *first* (fastest) axis.
    if (nrrdKind3DMaskedSymMatrix == nrrd->axis[0].kind
        && ImageIOBase::SYMMETRICSECONDRANKTENSOR == this->GetPixelType())
      {
      // we crop out the mask and put the output in ITK-allocated "buffer"
      size_t size[NRRD_DIM_MAX], minIdx[NRRD_DIM_MAX], maxIdx[NRRD_DIM_MAX];
      for (unsigned int axi=0; axi<nrrd->dim; axi++)
        {
        minIdx[axi] = (0 == axi) ? 1 : 0;
        maxIdx[axi] = nrrd->axis[axi].size-1;
        size[axi] = maxIdx[axi] - minIdx[axi] + 1;
        }
      Nrrd *ntmp = nrrdNew();
      if (nrrdCopy(ntmp, nrrd)
          || (nrrdEmpty(nrrd), 
              nrrdWrap_nva(nrrd, buffer, ntmp->type, ntmp->dim, size))
          || nrrdCrop(nrrd, ntmp, minIdx, maxIdx))
        {
        char *err =  biffGetDone(NRRD); // would be nice to free(err)
        itkExceptionMacro("Read: Error copying, crapping or cropping:\n"
                          << err);
        }
      nrrdNuke(ntmp);
      nrrdNix(nrrd);
      }
    else
      {

      // false alarm; we didn't need to allocate the data ourselves
      memcpy(buffer, nrrd->data,
             nrrdElementSize(nrrd)*nrrdElementNumber(nrrd));
      nrrdNuke(nrrd);
      }
    }
  else // 
    {

    // "buffer" == nrrd->data was ITK-allocated; lose the nrrd struct
    nrrdNix(nrrd);
    }
} 


bool NrrdImageIO::CanWriteFile( const char * name )
{
#if defined(__BORLANDC__) 
// Disable floating point exceptions in Borland 
  _control87(MCW_EM, MCW_EM); 
#endif // defined(__BORLANDC__) 

  std::string filename = name;
  if(  filename == "" )
    {
    return false;
    }

  std::string::size_type nrrdPos = filename.rfind(".nrrd");
  if ((nrrdPos != std::string::npos)
      && (nrrdPos == filename.length() - 5))
    {
    return true;
    }

  std::string::size_type nhdrPos = filename.rfind(".nhdr");
  if ((nhdrPos != std::string::npos)
      && (nhdrPos == filename.length() - 5))
    {
    return true;
    }

  return false;
}
  
void NrrdImageIO::WriteImageInformation(void)
{
#if defined(__BORLANDC__) 
// Disable floating point exceptions in Borland 
  _control87(MCW_EM, MCW_EM); 
#endif // defined(__BORLANDC__) 

  // Nothing needs doing here.
}


void NrrdImageIO::Write( const void* buffer) 
{
#if defined(__BORLANDC__) 
// Disable floating point exceptions in Borland 
  _control87(MCW_EM, MCW_EM); 
#endif // defined(__BORLANDC__) 

  Nrrd *nrrd = nrrdNew();
  NrrdIoState *nio = nrrdIoStateNew();
  int kind[NRRD_DIM_MAX];
  size_t size[NRRD_DIM_MAX];
  unsigned int nrrdDim, baseDim, spaceDim;
  double spaceDir[NRRD_DIM_MAX][NRRD_SPACE_DIM_MAX];
  double origin[NRRD_DIM_MAX];

  spaceDim = this->GetNumberOfDimensions();
  if (this->GetNumberOfComponents() > 1)
    {
    size[0] = this->GetNumberOfComponents();
    switch (this->GetPixelType())
      {
      case ImageIOBase::RGB:
        kind[0] = nrrdKindRGBColor;
        break;
      case ImageIOBase::RGBA:
        kind[0] = nrrdKindRGBAColor;
        break;
      case ImageIOBase::POINT:
        kind[0] = nrrdKindPoint;
        break;
      case ImageIOBase::COVARIANTVECTOR:
        kind[0] = nrrdKindCovariantVector;
        break;
      case ImageIOBase::SYMMETRICSECONDRANKTENSOR:
      case ImageIOBase::DIFFUSIONTENSOR3D:
        kind[0] = nrrdKind3DSymMatrix;
        break;
      case ImageIOBase::COMPLEX:
        kind[0] = nrrdKindComplex;
        break;
      case ImageIOBase::VECTOR:
      case ImageIOBase::OFFSET:      // HEY is this right?
      case ImageIOBase::FIXEDARRAY:  // HEY is this right?
      default:
        kind[0] = nrrdKindVector;
        break;
      }
    // the range axis has no space direction
    for (unsigned int saxi=0; saxi < spaceDim; saxi++)
      {
      spaceDir[0][saxi] = AIR_NAN;
      }
    baseDim = 1;
    }
  else
    {
    baseDim = 0;
    }
  nrrdDim = baseDim + spaceDim;
  std::vector<double> spaceDirStd(spaceDim);
  unsigned int axi;
  for (axi=0; axi < spaceDim; axi++)
    {
    size[axi+baseDim] = this->GetDimensions(axi);
    kind[axi+baseDim] = nrrdKindDomain;
    origin[axi] = this->GetOrigin(axi);
    double spacing = this->GetSpacing(axi);
    spaceDirStd = this->GetDirection(axi);
    for (unsigned int saxi=0; saxi < spaceDim; saxi++)
      {
      spaceDir[axi+baseDim][saxi] = spacing*spaceDirStd[saxi];
      }
    }
  if (nrrdWrap_nva(nrrd, const_cast<void *>(buffer),
                   this->ITKToNrrdComponentType( m_ComponentType ),
                   nrrdDim, size) || (3 == spaceDim
       // special case: ITK is LPS in 3-D
       ? nrrdSpaceSet(nrrd, nrrdSpaceLeftPosteriorSuperior)
       : nrrdSpaceDimensionSet(nrrd, spaceDim)) ||
      nrrdSpaceOriginSet(nrrd, origin))
    {
    char *err = biffGetDone(NRRD); // would be nice to free(err)
    itkExceptionMacro("Write: Error wrapping nrrd for " 
                      << this->GetFileName() << ":\n" << err);
    }
  nrrdAxisInfoSet_nva(nrrd, nrrdAxisInfoKind, kind);
  nrrdAxisInfoSet_nva(nrrd, nrrdAxisInfoSpaceDirection, spaceDir);

  // Go through MetaDataDictionary and set either specific nrrd field
  // or a key/value pair
  MetaDataDictionary &thisDic = this->GetMetaDataDictionary();
  std::vector<std::string> keys = thisDic.GetKeys();
  std::vector<std::string>::const_iterator keyIt;
  const char *keyField, *field;
  for( keyIt = keys.begin(); keyIt != keys.end(); keyIt++ )
    {
    if (!strncmp(KEY_PREFIX, (*keyIt).c_str(), strlen(KEY_PREFIX)))
      {
      keyField = (*keyIt).c_str() + strlen(KEY_PREFIX);
      // only of one of these can succeed
      field = airEnumStr(nrrdField, nrrdField_thicknesses);
      if (!strncmp(keyField, field, strlen(field)))
        {
        if (1 == sscanf(keyField + strlen(field), "[%d]", &axi)
            && axi + baseDim < nrrd->dim)
          {
          double thickness = 0.0;  // local for Borland
          ExposeMetaData<double>(thisDic, *keyIt, thickness);
          nrrd->axis[axi+baseDim].thickness = thickness;
          }
        }
      field = airEnumStr(nrrdField, nrrdField_centers);
      if (!strncmp(keyField, field, strlen(field)))
        {
        if (1 == sscanf(keyField + strlen(field), "[%d]", &axi)
            && axi + baseDim < nrrd->dim)
          {
          std::string value;  // local for Borland
          ExposeMetaData<std::string>(thisDic, *keyIt, value);
          nrrd->axis[axi+baseDim].center = airEnumVal(nrrdCenter,
                                                      value.c_str());
          }
        }
      field = airEnumStr(nrrdField, nrrdField_kinds);
      if (!strncmp(keyField, field, strlen(field)))
        {
        if (1 == sscanf(keyField + strlen(field), "[%d]", &axi)
            && axi + baseDim < nrrd->dim)
          {
          std::string value;  // local for Borland
          ExposeMetaData<std::string>(thisDic, *keyIt, value);
          nrrd->axis[axi+baseDim].kind = airEnumVal(nrrdKind,
                                                    value.c_str());
          }
        }
      field = airEnumStr(nrrdField, nrrdField_labels);
      if (!strncmp(keyField, field, strlen(field)))
        {
        if (1 == sscanf(keyField + strlen(field), "[%d]", &axi)
            && axi + baseDim < nrrd->dim)
          {
          std::string value;  // local for Borland
          ExposeMetaData<std::string>(thisDic, *keyIt, value);
          nrrd->axis[axi+baseDim].label = airStrdup(value.c_str());
          }
        }
      field = airEnumStr(nrrdField, nrrdField_old_min);
      if (!strncmp(keyField, field, strlen(field)))
        {
        ExposeMetaData<double>(thisDic, *keyIt, nrrd->oldMin);
        }
      field = airEnumStr(nrrdField, nrrdField_old_max);
      if (!strncmp(keyField, field, strlen(field)))
        {
        ExposeMetaData<double>(thisDic, *keyIt, nrrd->oldMax);
        }

      field = airEnumStr(nrrdField, nrrdField_space);
      if (!strncmp(keyField, field, strlen(field)))
        {
        int space;
        std::string value;  // local for Borland
        ExposeMetaData<std::string>(thisDic, *keyIt, value);
        space = airEnumVal(nrrdSpace, value.c_str());
        if (nrrdSpaceDimension(space) == nrrd->spaceDim)
          {
          // sanity check
          nrrd->space = space;
          }
        }

      field = airEnumStr(nrrdField, nrrdField_content);
      if (!strncmp(keyField, field, strlen(field)))
        {
        std::string value;  // local for Borland
        ExposeMetaData<std::string>(thisDic, *keyIt, value);
        nrrd->content = airStrdup(value.c_str());
        }
      field = airEnumStr(nrrdField, nrrdField_measurement_frame);
      if (!strncmp(keyField, field, strlen(field)))
        {
        std::vector<std::vector<double> > msrFrame;
        ExposeMetaData<std::vector<std::vector<double> > >(thisDic,
                                                           *keyIt, msrFrame);
        for (unsigned int saxi=0; saxi < nrrd->spaceDim; saxi++)
          {
          for (unsigned int saxj=0; saxj < nrrd->spaceDim; saxj++)
            {
            if (saxi < msrFrame.size() &&
                saxj < msrFrame[saxi].size())
              {
              nrrd->measurementFrame[saxi][saxj] = msrFrame[saxi][saxj];
              }
            else
              {
              // there is a difference between the dimension of the 
              // recorded measurement frame, and the actual dimension of
              // the ITK image, which (for now) determines nrrd->spaceDim.
              // We can't set this to AIR_NAN, because the coefficients of
              // the measurement frame have to all be equally existent.
              // If we used 0, it might not a flag that something is wrong.
              // So, we have to get creative.
              nrrd->measurementFrame[saxi][saxj] = 666666;
              }
            }
          }
        }
      }
    else
      {
      // not a NRRD field packed into meta data; just a regular key/value
      std::string value;  // local for Borland
      ExposeMetaData<std::string>(thisDic, *keyIt, value);
      nrrdKeyValueAdd(nrrd, (*keyIt).c_str(), value.c_str());
      }
    }

  // set encoding for data: compressed (raw), (uncompressed) raw, or ascii
  if (this->GetUseCompression() == true
      && nrrdEncodingGzip->available())
    {
    // this is necessarily gzip-compressed *raw* data
    nio->encoding = nrrdEncodingGzip;
    }
  else
    {
    Superclass::FileType fileType = this->GetFileType();
    switch ( fileType )
      {
      default:
      case TypeNotApplicable:
      case Binary:
        nio->encoding = nrrdEncodingRaw;
        break;
      case ASCII:
        nio->encoding = nrrdEncodingAscii;
        break;
      }
    }

  // set desired endianness of output
  Superclass::ByteOrder byteOrder = this->GetByteOrder();
  switch (byteOrder)
    {
    default:
    case OrderNotApplicable:
      nio->endian = airEndianUnknown;
      break;
    case BigEndian:
      nio->endian = airEndianBig;
      break;
    case LittleEndian:
      nio->endian = airEndianLittle;
      break;
    }
  
  // Write the nrrd to file.
  if (nrrdSave(this->GetFileName(), nrrd, nio))
    {
    char *err = biffGetDone(NRRD); // would be nice to free(err)
    itkExceptionMacro("Write: Error writing " 
                      << this->GetFileName() << ":\n" << err);
    }
  
  // Free the nrrd struct but don't touch nrrd->data
  nrrd = nrrdNix(nrrd);
  nio = nrrdIoStateNix(nio);
}
 
} // end namespace itk