/usr/include/CiftiLib/NiftiIO.h is in libcifti-dev 1.5.1-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #ifndef __NIFTI_IO_H__
#define __NIFTI_IO_H__
/*LICENSE_START*/
/*
* Copyright (c) 2014, Washington University School of Medicine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Common/AString.h"
#include "Common/ByteSwapping.h"
#include "Common/BinaryFile.h"
#include "Common/CiftiException.h"
#include "Common/CiftiMutex.h"
#include "Nifti/NiftiHeader.h"
//include MultiDimIterator from a private include directory, in case people want to use it with NiftiIO
#include "Common/MultiDimIterator.h"
#include <cmath>
#include <limits>
#include <vector>
namespace cifti
{
class NiftiIO
{
BinaryFile m_file;
NiftiHeader m_header;
std::vector<int64_t> m_dims;
std::vector<char> m_scratch;//scratch memory for byteswapping, type conversion, etc
CiftiMutex m_mutex;
int numBytesPerElem();//for resizing scratch
template<typename TO, typename FROM>
void convertRead(TO* out, FROM* in, const int64_t& count);//for reading from file
template<typename TO, typename FROM>
void convertWrite(TO* out, const FROM* in, const int64_t& count);//for writing to file
public:
void openRead(const AString& filename);
void writeNew(const AString& filename, const NiftiHeader& header, const int& version = 1, const bool& withRead = false, const bool& swapEndian = false);
AString getFilename() const { return m_file.getFilename(); }
void overrideDimensions(const std::vector<int64_t>& newDims) { m_dims = newDims; }//HACK: deal with reading/writing CIFTI-1's broken headers
void close();
const NiftiHeader& getHeader() const { return m_header; }
const std::vector<int64_t>& getDimensions() const { return m_dims; }
int getNumComponents() const;
//to read/write 1 frame of a standard volume file, call with fullDims = 3, indexSelect containing indexes for any of dims 4-7 that exist
//NOTE: you need to provide storage for all components within the range, if getNumComponents() == 3 and fullDims == 0, you need 3 elements allocated
template<typename T>
void readData(T* dataOut, const int& fullDims, const std::vector<int64_t>& indexSelect, const bool& tolerateShortRead = false);
template<typename T>
void writeData(const T* dataIn, const int& fullDims, const std::vector<int64_t>& indexSelect);
};
template<typename T>
void NiftiIO::readData(T* dataOut, const int& fullDims, const std::vector<int64_t>& indexSelect, const bool& tolerateShortRead)
{
if (fullDims < 0) throw CiftiException("NiftiIO: fulldims must not be negative");
if (fullDims > (int)m_dims.size()) throw CiftiException("NiftiIO: fulldims must not be greater than number of dimensions");
if ((size_t)fullDims + indexSelect.size() != m_dims.size())
{//could be >=, but should catch more stupid mistakes as ==
throw CiftiException("NiftiIO: fulldims plus length of indexSelect must equal number of dimensions");
}
int64_t numElems = getNumComponents();//for now, calculate read size on the fly, as the read call will be the slowest part
int curDim;
for (curDim = 0; curDim < fullDims; ++curDim)
{
numElems *= m_dims[curDim];
}
int64_t numDimSkip = numElems, numSkip = 0;
for (; curDim < (int)m_dims.size(); ++curDim)
{
if (indexSelect[curDim - fullDims] < 0) throw CiftiException("NiftiIO: indices must not be negative");
if (indexSelect[curDim - fullDims] >= m_dims[curDim]) throw CiftiException("NiftiIO: index exceeds nifti dimension length");
numSkip += indexSelect[curDim - fullDims] * numDimSkip;
numDimSkip *= m_dims[curDim];
}
CiftiMutexLocker locked(&m_mutex);//protect starting with resizing until we are done converting, because we use an internal variable for scratch space
//we can't guarantee that the output memory is enough to use as scratch space, as we might be doing a narrowing conversion
//we are doing FILE ACCESS, so cpu performance isn't really something to worry about
m_scratch.resize(numElems * numBytesPerElem());
m_file.seek(numSkip * numBytesPerElem() + m_header.getDataOffset());
int64_t numRead = 0;
m_file.read(m_scratch.data(), m_scratch.size(), &numRead);
if ((numRead != (int64_t)m_scratch.size() && !tolerateShortRead) || numRead < 0)//for now, assume read giving -1 is always a problem
{
throw CiftiException("error while reading from file '" + m_file.getFilename() + "'");
}
switch (m_header.getDataType())
{
case NIFTI_TYPE_UINT8:
case NIFTI_TYPE_RGB24://handled by components
convertRead(dataOut, (uint8_t*)m_scratch.data(), numElems);
break;
case NIFTI_TYPE_INT8:
convertRead(dataOut, (int8_t*)m_scratch.data(), numElems);
break;
case NIFTI_TYPE_UINT16:
convertRead(dataOut, (uint16_t*)m_scratch.data(), numElems);
break;
case NIFTI_TYPE_INT16:
convertRead(dataOut, (int16_t*)m_scratch.data(), numElems);
break;
case NIFTI_TYPE_UINT32:
convertRead(dataOut, (uint32_t*)m_scratch.data(), numElems);
break;
case NIFTI_TYPE_INT32:
convertRead(dataOut, (int32_t*)m_scratch.data(), numElems);
break;
case NIFTI_TYPE_UINT64:
convertRead(dataOut, (uint64_t*)m_scratch.data(), numElems);
break;
case NIFTI_TYPE_INT64:
convertRead(dataOut, (int64_t*)m_scratch.data(), numElems);
break;
case NIFTI_TYPE_FLOAT32:
case NIFTI_TYPE_COMPLEX64://components
convertRead(dataOut, (float*)m_scratch.data(), numElems);
break;
case NIFTI_TYPE_FLOAT64:
case NIFTI_TYPE_COMPLEX128:
convertRead(dataOut, (double*)m_scratch.data(), numElems);
break;
case NIFTI_TYPE_FLOAT128:
case NIFTI_TYPE_COMPLEX256:
convertRead(dataOut, (long double*)m_scratch.data(), numElems);
break;
default:
throw CiftiException("internal error, tell the developers what you just tried to do");
}
}
template<typename T>
void NiftiIO::writeData(const T* dataIn, const int& fullDims, const std::vector<int64_t>& indexSelect)
{
if (fullDims < 0) throw CiftiException("NiftiIO: fulldims must not be negative");
if (fullDims > (int)m_dims.size()) throw CiftiException("NiftiIO: fulldims must not be greater than number of dimensions");
if ((size_t)fullDims + indexSelect.size() != m_dims.size())
{//could be >=, but should catch more stupid mistakes as ==
throw CiftiException("NiftiIO: fulldims plus length of indexSelect must equal number of dimensions");
}
int64_t numElems = getNumComponents();//for now, calculate read size on the fly, as the read call will be the slowest part
int curDim;
for (curDim = 0; curDim < fullDims; ++curDim)
{
numElems *= m_dims[curDim];
}
int64_t numDimSkip = numElems, numSkip = 0;
for (; curDim < (int)m_dims.size(); ++curDim)
{
if (indexSelect[curDim - fullDims] < 0) throw CiftiException("NiftiIO: indices must not be negative");
if (indexSelect[curDim - fullDims] >= m_dims[curDim]) throw CiftiException("NiftiIO: index exceeds nifti dimension length");
numSkip += indexSelect[curDim - fullDims] * numDimSkip;
numDimSkip *= m_dims[curDim];
}
CiftiMutexLocker locked(&m_mutex);//protect starting with resizing until we are done writing, because we use an internal variable for scratch space
//we are doing FILE ACCESS, so cpu performance isn't really something to worry about
m_scratch.resize(numElems * numBytesPerElem());
m_file.seek(numSkip * numBytesPerElem() + m_header.getDataOffset());
switch (m_header.getDataType())
{
case NIFTI_TYPE_UINT8:
case NIFTI_TYPE_RGB24://handled by components
convertWrite((uint8_t*)m_scratch.data(), dataIn, numElems);
break;
case NIFTI_TYPE_INT8:
convertWrite((int8_t*)m_scratch.data(), dataIn, numElems);
break;
case NIFTI_TYPE_UINT16:
convertWrite((uint16_t*)m_scratch.data(), dataIn, numElems);
break;
case NIFTI_TYPE_INT16:
convertWrite((int16_t*)m_scratch.data(), dataIn, numElems);
break;
case NIFTI_TYPE_UINT32:
convertWrite((uint32_t*)m_scratch.data(), dataIn, numElems);
break;
case NIFTI_TYPE_INT32:
convertWrite((int32_t*)m_scratch.data(), dataIn, numElems);
break;
case NIFTI_TYPE_UINT64:
convertWrite((uint64_t*)m_scratch.data(), dataIn, numElems);
break;
case NIFTI_TYPE_INT64:
convertWrite((int64_t*)m_scratch.data(), dataIn, numElems);
break;
case NIFTI_TYPE_FLOAT32:
case NIFTI_TYPE_COMPLEX64://components
convertWrite((float*)m_scratch.data(), dataIn, numElems);
break;
case NIFTI_TYPE_FLOAT64:
case NIFTI_TYPE_COMPLEX128:
convertWrite((double*)m_scratch.data(), dataIn, numElems);
break;
case NIFTI_TYPE_FLOAT128:
case NIFTI_TYPE_COMPLEX256:
convertWrite((long double*)m_scratch.data(), dataIn, numElems);
break;
default:
throw CiftiException("internal error, tell the developers what you just tried to do");
}
m_file.write(m_scratch.data(), m_scratch.size());
}
template<typename TO, typename FROM>
void NiftiIO::convertRead(TO* out, FROM* in, const int64_t& count)
{
if (m_header.isSwapped())
{
ByteSwapping::swapArray(in, count);
}
double mult, offset;
bool doScale = m_header.getDataScaling(mult, offset);
if (std::numeric_limits<TO>::is_integer)//do round to nearest when integer output type
{
if (doScale)
{
for (int64_t i = 0; i < count; ++i)
{
out[i] = (TO)floor(0.5 + offset + mult * (long double)in[i]);//we don't always need that much precision, but it will still be faster than hard drives
}
} else {
for (int64_t i = 0; i < count; ++i)
{
out[i] = (TO)floor(0.5 + in[i]);
}
}
} else {
if (doScale)
{
for (int64_t i = 0; i < count; ++i)
{
out[i] = (TO)(offset + mult * (long double)in[i]);//we don't always need that much precision, but it will still be faster than hard drives
}
} else {
for (int64_t i = 0; i < count; ++i)
{
out[i] = (TO)in[i];//explicit cast to make sure the compiler doesn't squawk
}
}
}
}
template<typename TO, typename FROM>
void NiftiIO::convertWrite(TO* out, const FROM* in, const int64_t& count)
{
double mult, offset;
bool doScale = m_header.getDataScaling(mult, offset);
if (std::numeric_limits<TO>::is_integer)//do round to nearest when integer output type
{
if (doScale)
{
for (int64_t i = 0; i < count; ++i)
{
out[i] = (TO)floor(0.5 + ((long double)in[i] - offset) / mult);//we don't always need that much precision, but it will still be faster than hard drives
}
} else {
for (int64_t i = 0; i < count; ++i)
{
out[i] = (TO)floor(0.5 + in[i]);
}
}
} else {
if (doScale)
{
for (int64_t i = 0; i < count; ++i)
{
out[i] = (TO)(((long double)in[i] - offset) / mult);//we don't always need that much precision, but it will still be faster than hard drives
}
} else {
for (int64_t i = 0; i < count; ++i)
{
out[i] = (TO)in[i];//explicit cast to make sure the compiler doesn't squawk
}
}
}
if (m_header.isSwapped()) ByteSwapping::swapArray(out, count);
}
}
#endif //__NIFTI_IO_H__
|