CiftiLib
A C++ library for CIFTI-2 and CIFTI-1 files
NiftiHeader.h
1 #ifndef __NIFTI_HEADER_H__
2 #define __NIFTI_HEADER_H__
3 
4 /*LICENSE_START*/
5 /*
6  * Copyright (c) 2014, Washington University School of Medicine
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "Common/BinaryFile.h"
32 
33 #include "nifti1.h"
34 #include "nifti2.h"
35 
36 #include "boost/shared_ptr.hpp"
37 #include <vector>
38 
39 namespace cifti
40 {
41 
43  {
44  int32_t m_ecode;
45  std::vector<char> m_bytes;
46  };
47 
48  struct NiftiHeader
49  {
50  std::vector<boost::shared_ptr<NiftiExtension> > m_extensions;//allow direct access to the extensions
51 
52  NiftiHeader();
53  void read(BinaryFile& inFile);
54  void write(BinaryFile& outFile, const int& version = 1, const bool& swapEndian = false);
55  bool canWriteVersion(const int& version) const;
56  bool isSwapped() const { return m_isSwapped; }
57  int version() const { return m_version; }
58 
59  std::vector<int64_t> getDimensions() const;
60  std::vector<std::vector<float> > getSForm() const;
61  double getTimeStep() const;//seconds
62  int64_t getDataOffset() const { return m_header.vox_offset; }
63  int16_t getDataType() const { return m_header.datatype; }
64  int32_t getIntentCode() const { return m_header.intent_code; }
65  const char* getIntentName() const { return m_header.intent_name; }//NOTE: 16 BYTES, MAY NOT HAVE A NULL TERMINATOR
66  const char* getDescription() const { return m_header.descrip; }//NOTE: 80 BYTES, MAY NOT HAVE A NULL TERMINATOR
67  bool getDataScaling(double& mult, double& offset) const;//returns false if scaling not needed
68  int getNumComponents() const;
69  AString toString() const;
70 
71  void setDimensions(const std::vector<int64_t>& dimsIn);
72  void setSForm(const std::vector<std::vector<float> > &sForm);
73  void setTimeStep(const double& seconds);
74  void setIntent(const int32_t& code, const char name[16]);
75  void setDescription(const char descrip[80]);
76  void setDataType(const int16_t& type);
77  void clearDataScaling();
78  void setDataScaling(const double& mult, const double& offset);
79  void setDataTypeAndScaleRange(const int16_t& type, const double& minval, const double& maxval);
81  std::vector<std::vector<float> > getFSLSpace() const;
82 
83  bool operator==(const NiftiHeader& rhs) const;//for testing purposes
84  bool operator!=(const NiftiHeader& rhs) const { return !((*this) == rhs); }
85  private:
86  struct Quirks
87  {
88  bool no_extender;
89  Quirks() { no_extender = false; }
90  };
91  nifti_2_header m_header;//storage for header values regardless of version
92  int m_version;
93  bool m_isSwapped;
94  static void swapHeaderBytes(nifti_1_header &header);
95  static void swapHeaderBytes(nifti_2_header &header);
96  void prepareHeader(nifti_1_header& header) const;//transform internal state into ready to write header struct
97  void prepareHeader(nifti_2_header& header) const;
98  Quirks setupFrom(const nifti_1_header& header, const AString& filename);//error check provided header, and populate members from it
99  Quirks setupFrom(const nifti_2_header& header, const AString& filename);
100  static int typeToNumBits(const int64_t& type);
101  int64_t computeVoxOffset(const int& version) const;
102  };
103 
104 }
105 
106 #endif //__NIFTI_HEADER_H__
cifti::BinaryFile
Definition: BinaryFile.h:41
cifti::nifti_2_header::vox_offset
int64_t vox_offset
Definition: nifti2.h:88
cifti::NiftiHeader::getFSLSpace
std::vector< std::vector< float > > getFSLSpace() const
get the FSL "scale" space
Definition: NiftiHeader.cxx:144
cifti::nifti_2_header::intent_name
char intent_name[16]
Definition: nifti2.h:113
cifti::nifti_1_header
Data structure defining the fields in the nifti1 header. This binary header should be found at the be...
Definition: nifti1.h:163
cifti::nifti_2_header
Data structure defining the fields in the nifti2 header. This binary header should be found at the be...
Definition: nifti2.h:77
cifti::NiftiExtension
Definition: NiftiHeader.h:43
cifti::nifti_2_header::datatype
int16_t datatype
Definition: nifti2.h:81
cifti::NiftiHeader
Definition: NiftiHeader.h:49
cifti::nifti_2_header::descrip
char descrip[80]
Definition: nifti2.h:97
cifti
namespace for all CiftiLib functionality
Definition: CiftiBrainModelsMap.h:42
cifti::nifti_2_header::intent_code
int32_t intent_code
Definition: nifti2.h:112