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  int64_t getDataOffset() const { return m_header.vox_offset; }
62  int16_t getDataType() const { return m_header.datatype; }
63  int32_t getIntentCode() const { return m_header.intent_code; }
64  const char* getIntentName() const { return m_header.intent_name; }//NOTE: 16 BYTES, MAY NOT HAVE A NULL TERMINATOR
65  bool getDataScaling(double& mult, double& offset) const;//returns false if scaling not needed
66  AString toString() const;
67 
68  void setDimensions(const std::vector<int64_t>& dimsIn);
69  void setSForm(const std::vector<std::vector<float> > &sForm);
70  void setIntent(const int32_t& code, const char name[16]);
71  void setDataType(const int16_t& type);
72  void clearDataScaling();
73  void setDataScaling(const double& mult, const double& offset);
75  std::vector<std::vector<float> > getFSLSpace() const;
76 
77  bool operator==(const NiftiHeader& rhs) const;//for testing purposes
78  bool operator!=(const NiftiHeader& rhs) const { return !((*this) == rhs); }
79  private:
80  nifti_2_header m_header;//storage for header values regardless of version
81  int m_version;
82  bool m_isSwapped;
83  static void swapHeaderBytes(nifti_1_header &header);
84  static void swapHeaderBytes(nifti_2_header &header);
85  void prepareHeader(nifti_1_header& header) const;//transform internal state into ready to write header struct
86  void prepareHeader(nifti_2_header& header) const;
87  void setupFrom(const nifti_1_header& header);//error check provided header, and populate members from it
88  void setupFrom(const nifti_2_header& header);
89  static int typeToNumBits(const int64_t& type);
90  int64_t computeVoxOffset(const int& version) const;
91  };
92 
93 }
94 
95 #endif //__NIFTI_HEADER_H__
Definition: NiftiHeader.h:42
namespace for all CiftiLib functionality
Definition: CiftiBrainModelsMap.h:41
Data structure defining the fields in the nifti1 header. This binary header should be found at the be...
Definition: nifti1.h:163
Data structure defining the fields in the nifti2 header. This binary header should be found at the be...
Definition: nifti2.h:77
Definition: NiftiHeader.h:48
Definition: BinaryFile.h:40