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
|
/*============================================================================
MetaIO
Copyright 2000-2010 Insight Software Consortium
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#include "metaTypes.h"
#ifndef ITKMetaIO_METAIMAGE_H
# define ITKMetaIO_METAIMAGE_H
# include "metaUtils.h"
# include "metaObject.h"
# include "metaImageTypes.h"
# include "metaImageUtils.h"
/*! MetaImage (.h and .cpp)
*
* Description:
* Reads and Writes MetaImageFiles.
* MetaImageFiles can be in one of two possible formats:
* a combined header/data format, typically designated .mha files
* or as separate header and data files, typically designated
* .mhd and .mda files
*
* Features:
* Header information is in ascii format - for easy creation, editing,
* and review. If separate files are used, a specified number of
* header-bytes in the datafile can be skipped
* - in this manner, different file formats (e.g., bmp, img,
* and /usr/Image) can be converted to metaImageFile format by
* merely specifying a separate text header (.mhd) file and in that
* file specifying how many data-header-bytes should be skipped.
* Thereby the data files can serve a dual role (as bitmap files
* and as metaImage data files)
* Supports all pixel formats
* Handles byte ordering (MSB/LSB)
* Provides the ability to handle associated medical image
* information (element size, element spacing, etc).
* Has required and optional header data (provides rapid formation
* or extensive documentation).
* REQUIRED: NDims, DimSize, ByteOrderMSB, ElementDataType, DataFileName
*
* \author Stephen R. Aylward
*
* \date August 29, 1999
*
* Depends on:
* MetaUtils.h
* MetaFileLib.h
*/
# if (METAIO_USE_NAMESPACE)
namespace METAIO_NAMESPACE
{
# endif
class METAIO_EXPORT MetaImage : public MetaObject
{
public:
// Constructors & Destructor
MetaImage();
explicit MetaImage(const char * _headerName);
explicit MetaImage(MetaImage * _im); // share memory
MetaImage(int _nDims,
const int * _dimSize,
const float * _elementSpacing,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels = 1,
void * _elementData = nullptr);
MetaImage(int _nDims,
const int * _dimSize,
const double * _elementSpacing,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels = 1,
void * _elementData = nullptr);
MetaImage(int _x,
int _y,
double _elementSpacingX,
double _elementSpacingY,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels = 1,
void * _elementData = nullptr);
MetaImage(int _x,
int _y,
int _z,
double _elementSpacingX,
double _elementSpacingY,
double _elementSpacingZ,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels = 1,
void * _elementData = nullptr);
~MetaImage() override;
void
PrintInfo() const override;
void
CopyInfo(const MetaObject * _object) override;
void
Clear() override;
// Legacy for floating point elementSpacing
bool
InitializeEssential(int _nDims,
const int * _dimSize,
const float * _elementSpacing,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels = 1,
void * _elementData = nullptr,
bool _allocElementMemory = true);
bool
InitializeEssential(int _nDims,
const int * _dimSize,
const double * _elementSpacing,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels = 1,
void * _elementData = nullptr,
bool _allocElementMemory = true);
int
HeaderSize() const;
void
HeaderSize(int _headerSize);
MET_ImageModalityEnumType
Modality() const;
void
Modality(MET_ImageModalityEnumType _modality);
// DimSize(...)
// REQUIRED Field
// Number of elements along each dimension
const int *
DimSize() const;
int
DimSize(int _i) const;
// void DimSize(const int * _dimSize);
// void DimSize(int _i, int _value);
// Quantity()
// Not a field in file
// Total number of elements in image (Prod(dimSize[i]))
std::streamoff
Quantity() const;
// SubQuantity(...)
// Not a field in file
// Number of elements in image spanning sub-dimensions
// E.g., elements per line, 2D sub-image, 3D sub-volume,
const std::streamoff *
SubQuantity() const;
std::streamoff
SubQuantity(int _i) const;
// SequenceID(...)
// Optional Field
// DICOM designation of this image relative to other images
// acquired at the same time
const float *
SequenceID() const;
float
SequenceID(int _i) const;
void
SequenceID(const float * _sequenceID);
void
SequenceID(int _i, float _value);
// ElemSize(...)
// Optional Field
// Physical size (in MM) of each element in the image
// (0 = xSize, 1 = ySize, 2 = zSize)
bool
ElementSizeValid() const;
void
ElementSizeValid(bool _elementSizeValid);
const double *
ElementSize() const;
double
ElementSize(int _i) const;
void
ElementSize(const double * _elementSize);
void
ElementSize(const float * _elementSize); // legacy
void
ElementSize(int _i, double _value);
MET_ValueEnumType
ElementType() const;
void
ElementType(MET_ValueEnumType _elementType);
int
ElementNumberOfChannels() const;
void
ElementNumberOfChannels(int _elementNumberOfChannels);
// ElemMakeByteOrderMSB(), ElemMakeByteOrderLSB(),
// ElemByteOrderSwap(), ElemByteOrderFix()
// The following functions are available only after
// ReadImageData() or if _read_and_close=TRUE when read
// if streaming is used, then the size of buffer in total number
// of elements, should be passed as an argument, otherwise the
// internal value Quantity() will be used
void
ElementByteOrderSwap(std::streamoff _quantity = 0);
bool
ElementByteOrderFix(std::streamoff _quantity = 0);
// Min(...) Max(...)
// The default max returned is the largest allowed by
// ElemNBytes (12 bit uint16_t will give 4096 max).
// This may not represent the true max. Use _reCalc=true
// to force a calcuation of the actual max element value.
bool
ElementMinMaxValid() const;
void
ElementMinMaxValid(bool _elementMinMaxValid);
void
ElementMinMaxRecalc();
double
ElementMin() const;
void
ElementMin(double _elementMin);
double
ElementMax() const;
void
ElementMax(double _elementMax);
double
ElementToIntensityFunctionSlope() const;
void
ElementToIntensityFunctionSlope(double _elementToIntensityFunctionSlope);
double
ElementToIntensityFunctionOffset() const;
void
ElementToIntensityFunctionOffset(double _elementOffset);
bool
AutoFreeElementData() const;
void
AutoFreeElementData(bool _autoFreeElementData);
const char *
ElementDataFileName() const;
void
ElementDataFileName(const char * _elementDataFileName);
void *
ElementData();
double
ElementData(std::streamoff _i) const;
bool
ElementData(std::streamoff _i, double _v);
void
ElementData(void * _elementData, bool _autoFreeElementData = false);
// ConverTo(...)
// Converts to a new data type
// Rescales using Min and Max (see above)
bool
ConvertElementDataTo(MET_ValueEnumType _elementType = MET_USHORT, double _toMin = 0, double _toMax = 0);
bool
ConvertElementDataToIntensityData(MET_ValueEnumType _elementType = MET_SHORT);
bool
ConvertIntensityDataToElementData(MET_ValueEnumType _elementType = MET_USHORT);
static bool
CanRead(const char * _headerName = nullptr) ;
bool
Read(const char * _headerName = nullptr ) override
{
return MetaImage::Read(_headerName, true, nullptr);
}
bool
Read(const char * _headerName, bool _readElements, void * _buffer = nullptr);
bool
ReadROI(int * _indexMin,
int * _indexMax,
const char * _headerName = nullptr,
bool _readElements = true,
void * _buffer = nullptr,
unsigned int subSamplingFactor = 1);
static bool
CanReadStream(std::ifstream * _stream) ;
bool
ReadStream(int _nDims, std::ifstream * _stream, bool _readElements = true, void * _buffer = nullptr);
bool
ReadROIStream(int * _indexMin,
int * _indexMax,
int _nDims,
std::ifstream * _stream,
bool _readElements = true,
void * _buffer = nullptr,
unsigned int subSamplingFactor = 1);
bool
Write(const char * _headName = nullptr) override
{
return MetaImage::Write(_headName, nullptr, true, nullptr, false);
}
bool
Write(const char * _headName,
const char * _dataName,
bool _writeElements = true,
const void * _constElementData = nullptr,
bool _append = false);
bool
WriteROI(int * _indexMin,
int * _indexMax,
const char * _headName = nullptr,
const char * _dataName = nullptr,
bool _writeElements = true,
const void * _constElementData = nullptr,
bool _append = false);
bool
WriteStream(std::ofstream * _stream, bool _writeElements = true, const void * _constElementData = nullptr);
bool
Append(const char * _headName = nullptr) override;
typedef std::pair<long, long> CompressionOffsetType;
// PROTECTED
protected:
MET_ImageModalityEnumType m_Modality;
MET_CompressionTableType * m_CompressionTable{};
int m_DimSize[10]{};
std::streamoff m_SubQuantity[10]{};
std::streamoff m_Quantity{};
int m_HeaderSize{};
float m_SequenceID[4]{};
bool m_ElementSizeValid{};
double m_ElementSize[10]{};
MET_ValueEnumType m_ElementType;
int m_ElementNumberOfChannels{};
bool m_ElementMinMaxValid{};
double m_ElementMin{};
double m_ElementMax{};
double m_ElementToIntensityFunctionSlope{};
double m_ElementToIntensityFunctionOffset{};
bool m_AutoFreeElementData{};
void * m_ElementData{};
std::string m_ElementDataFileName;
void
M_ResetValues();
void
M_SetupReadFields() override;
void
M_SetupWriteFields() override;
bool
M_Read() override;
// _dataQuantity is expressed in number of pixels. Internally it will be
// scaled by the number of components and number of bytes per component.
bool
M_ReadElements(std::ifstream * _fstream, void * _data, std::streamoff _dataQuantity);
// _totalDataQuantity and _dataQuantity are expressed in number of pixels.
// Internally they will be scaled by the number of components and number of
// bytes per component.
bool
M_ReadElementsROI(std::ifstream * _fstream,
void * _data,
std::streamoff _dataQuantity,
int * _indexMin,
int * _indexMax,
unsigned int subSamplingFactor = 1,
std::streamoff _totalDataQuantity = 0);
bool
M_ReadElementData(std::ifstream * _fstream, void * _data, std::streamoff _dataQuantity);
bool
M_WriteElements(std::ofstream * _fstream, const void * _data, std::streamoff _dataQuantity);
bool
M_WriteElementsROI(std::ofstream * _fstream,
const void * _data,
std::streampos _dataPos,
const int * _indexMin,
const int * _indexMax);
bool
M_WriteElementData(std::ofstream * _fstream, const void * _data, std::streamoff _dataQuantity);
static bool
M_FileExists(const char * filename) ;
static bool
FileIsFullPath(const char * in_name) ;
static std::string
M_GetTagValue(const std::string & buffer, const char * tag) ;
// PRIVATE
private:
void
InitHelper(int _nDims,
const int * _dimSize,
const double * _elementSpacing,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels,
void * _elementData);
};
# if (METAIO_USE_NAMESPACE)
};
# endif
#endif
|