CiftiFile.h
#ifndef __CIFTI_FILE_H__
#define __CIFTI_FILE_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/CiftiException.h"
#include "Common/MultiDimIterator.h"
#include "Cifti/CiftiXML.h"
#include "Nifti/nifti1.h"
#include "boost/shared_ptr.hpp"
#include <vector>
namespace cifti
{
class CiftiFile
{
public:
enum ENDIAN
{
ANY,//so that writeFile() with default endian argument can do nothing after setWritingFile with any endian argument - uses native if there is no rewrite to avoid
NATIVE,//as long as there are more than two options anyway, provide a convenience option so people don't need to figure out the machine endianness for a common case
LITTLE,
BIG
};
CiftiFile();
explicit CiftiFile(const AString &fileName);
void openFile(const AString& fileName);
void setWritingFile(const AString& fileName, const CiftiVersion& writingVersion = CiftiVersion(), const ENDIAN& endian = NATIVE);
void writeFile(const AString& fileName, const CiftiVersion& writingVersion = CiftiVersion(), const ENDIAN& endian = ANY);
void close();
void convertToInMemory();
const CiftiXML& getCiftiXML() const { return m_xml; }
bool isInMemory() const;
void getRow(float* dataOut, const std::vector<int64_t>& indexSelect, const bool& tolerateShortRead = false) const;
const std::vector<int64_t>& getDimensions() const { return m_dims; }
MultiDimIterator<int64_t> getIteratorOverRows() const
{
return MultiDimIterator<int64_t>(std::vector<int64_t>(m_dims.begin() + 1, m_dims.end()));
}
void getColumn(float* dataOut, const int64_t& index) const;
void setCiftiXML(const CiftiXML& xml, const bool useOldMetadata = true);
void setRow(const float* dataIn, const std::vector<int64_t>& indexSelect);
void setColumn(const float* dataIn, const int64_t& index);
void getRow(float* dataOut, const int64_t& index, const bool& tolerateShortRead = false) const;
void setRow(const float* dataIn, const int64_t& index);
void setWritingDataTypeNoScaling(const int16_t& type = NIFTI_TYPE_FLOAT32);
void setWritingDataTypeAndScaling(const int16_t& type, const double& minval, const double& maxval);
//implementation details from here down
class ReadImplInterface
{
public:
virtual void getRow(float* dataOut, const std::vector<int64_t>& indexSelect, const bool& tolerateShortRead) const = 0;
virtual void getColumn(float* dataOut, const int64_t& index) const = 0;
virtual bool isInMemory() const { return false; }
virtual ~ReadImplInterface();
};
//assume if you can write to it, you can also read from it
class WriteImplInterface : public ReadImplInterface
{
public:
virtual void setRow(const float* dataIn, const std::vector<int64_t>& indexSelect) = 0;
virtual void setColumn(const float* dataIn, const int64_t& index) = 0;
virtual void close() {}
virtual ~WriteImplInterface();
};
private:
std::vector<int64_t> m_dims;
boost::shared_ptr<WriteImplInterface> m_writingImpl;//this will be equal to m_readingImpl when non-null
boost::shared_ptr<ReadImplInterface> m_readingImpl;
AString m_writingFile;
CiftiXML m_xml;
CiftiVersion m_onDiskVersion;
ENDIAN m_endianPref;
bool m_doWriteScaling;
int16_t m_writingDataType;
double m_minScalingVal, m_maxScalingVal;
void verifyWriteImpl();
static void copyImplData(const ReadImplInterface* from, WriteImplInterface* to, const std::vector<int64_t>& dims);
};
}
#endif //__CIFTI_FILE_H__
cifti::CiftiFile::ReadImplInterface
Definition CiftiFile.h:111
cifti::CiftiFile::WriteImplInterface
Definition CiftiFile.h:120
cifti::CiftiFile::getColumn
void getColumn(float *dataOut, const int64_t &index) const
for 2D only, will be slow if on disk!
Definition CiftiFile.cxx:262
cifti::CiftiFile::setWritingDataTypeNoScaling
void setWritingDataTypeNoScaling(const int16_t &type=NIFTI_TYPE_FLOAT32)
data type and scaling options - should be set before setRow, etc, to avoid rewriting of file
Definition CiftiFile.cxx:167
cifti::CiftiFile::getRow
void getRow(float *dataOut, const std::vector< int64_t > &indexSelect, const bool &tolerateShortRead=false) const
the tolerateShortRead parameter is useful for on-disk writing when it is easiest to do RMW multiple t...
Definition CiftiFile.cxx:255
cifti::CiftiFile::convertToInMemory
void convertToInMemory()
reads file into memory, closes file
Definition CiftiFile.cxx:231
cifti::CiftiFile::openFile
void openFile(const AString &fileName)
starts on-disk reading
Definition CiftiFile.cxx:149
cifti::CiftiFile::writeFile
void writeFile(const AString &fileName, const CiftiVersion &writingVersion=CiftiVersion(), const ENDIAN &endian=ANY)
does nothing if filename, version, and effective endianness match file currently open,...
Definition CiftiFile.cxx:185
cifti::CiftiFile::close
void close()
closes the underlying file to flush it, so that exceptions can be thrown
Definition CiftiFile.cxx:215
cifti::CiftiFile::setWritingFile
void setWritingFile(const AString &fileName, const CiftiVersion &writingVersion=CiftiVersion(), const ENDIAN &endian=NATIVE)
starts on-disk writing
Definition CiftiFile.cxx:159
cifti::CiftiFile::getIteratorOverRows
MultiDimIterator< int64_t > getIteratorOverRows() const
convenience function for iterating over arbitrary numbers of dimensions
Definition CiftiFile.h:85
cifti::CiftiFile::setColumn
void setColumn(const float *dataIn, const int64_t &index)
for 2D only, will be slow if on disk!
Definition CiftiFile.cxx:297
cifti::CiftiVersion
Definition CiftiVersion.h:38
cifti::CiftiXML
class for retrieving and setting mapping information of cifti files
Definition CiftiXML.h:49
cifti::MultiDimIterator
Definition MultiDimIterator.h:39
cifti::NIFTI_TYPE_FLOAT32
const int32_t NIFTI_TYPE_FLOAT32
Definition nifti1.h:552
cifti
namespace for all CiftiLib functionality
Definition CiftiBrainModelsMap.h:42