37 #ifndef VIGRA_MULTI_IMPEX_HXX
38 #define VIGRA_MULTI_IMPEX_HXX
48 #include "basicimageview.hxx"
50 #include "multi_array.hxx"
51 #include "multi_pointoperators.hxx"
78 typedef ImageImportInfo::PixelType PixelType;
90 VIGRA_EXPORT
VolumeImportInfo(
const std::string &baseName,
const std::string &extension);
153 VIGRA_EXPORT PixelType
pixelType()
const;
156 VIGRA_EXPORT
bool isGrayscale()
const;
157 VIGRA_EXPORT
bool isColor()
const;
160 VIGRA_EXPORT
const std::string &name()
const;
162 VIGRA_EXPORT
const std::string &description()
const;
164 template <
class T,
class Str
ide>
168 void getVolumeInfoFromFirstSlice(
const std::string &filename);
175 std::string path_, name_, description_, pixelType_;
177 std::string rawFilename_;
178 std::string baseName_, extension_;
179 std::vector<std::string> numbers_;
214 VIGRA_EXPORT
VolumeExportInfo(
const char * name_base,
const char * name_ext );
215 VIGRA_EXPORT ~VolumeExportInfo();
220 VIGRA_EXPORT VolumeExportInfo &
setFileNameBase(
const char * name_base);
230 VIGRA_EXPORT VolumeExportInfo &
setFileNameExt(
const char * name_ext);
231 VIGRA_EXPORT
const char * getFileNameBase()
const;
232 VIGRA_EXPORT
const char * getFileNameExt()
const;
282 VIGRA_EXPORT VolumeExportInfo &
setFileType(
const char * );
283 VIGRA_EXPORT
const char * getFileType()
const;
302 VIGRA_EXPORT
const char * getCompression()
const;
315 VIGRA_EXPORT VolumeExportInfo &
setPixelType(
const char * );
328 VIGRA_EXPORT VolumeExportInfo & setForcedRangeMapping(
double fromMin,
double fromMax,
329 double toMin,
double toMax);
330 VIGRA_EXPORT
bool hasForcedRangeMapping()
const;
331 VIGRA_EXPORT
double getFromMin()
const;
332 VIGRA_EXPORT
double getFromMax()
const;
333 VIGRA_EXPORT
double getToMin()
const;
334 VIGRA_EXPORT
double getToMax()
const;
339 VIGRA_EXPORT
float getXResolution()
const;
344 VIGRA_EXPORT
float getYResolution()
const;
349 VIGRA_EXPORT
float getZResolution()
const;
387 float m_x_res, m_y_res, m_z_res;
389 std::string m_filetype, m_filename_base, m_filename_ext, m_pixeltype, m_comp;
392 double fromMin_, fromMax_, toMin_, toMax_;
397 template <
class DestIterator,
class Shape,
class T>
399 readVolumeImpl(DestIterator d, Shape
const & shape, std::ifstream & s,
ArrayVector<T> & buffer, MetaInt<0>)
401 s.read((
char*)buffer.
begin(), shape[0]*
sizeof(T));
403 DestIterator dend = d + shape[0];
405 for(; d < dend; ++d, k++)
411 template <
class DestIterator,
class Shape,
class T,
int N>
413 readVolumeImpl(DestIterator d, Shape
const & shape, std::ifstream & s, ArrayVector<T> & buffer, MetaInt<N>)
415 DestIterator dend = d + shape[N];
418 readVolumeImpl(d.begin(), shape, s, buffer, MetaInt<N-1>());
424 template <
class T,
class Str
ide>
425 void VolumeImportInfo::importImpl(MultiArrayView <3, T, Stride> &volume)
const
427 vigra_precondition(this->shape() == volume.shape(),
"importVolume(): Volume must be shaped according to VolumeImportInfo.");
429 if(rawFilename_.size())
431 std::string dirName, baseName;
435 if(_getcwd(oldCWD, 2048) == 0)
438 vigra_fail(
"VolumeImportInfo: Unable to query current directory (getcwd).");
440 if(_chdir(path_.c_str()))
443 vigra_fail(
"VolumeImportInfo: Unable to change to new directory (chdir).");
446 if(getcwd(oldCWD, 2048) == 0)
449 vigra_fail(
"VolumeImportInfo: Unable to query current directory (getcwd).");
451 if(chdir(path_.c_str()))
454 vigra_fail(
"VolumeImportInfo: Unable to change to new directory (chdir).");
458 std::ifstream s(rawFilename_.c_str(), std::ios::binary);
459 vigra_precondition(s.good(),
"RAW file could not be opened");
461 ArrayVector<T> buffer(shape_[0]);
462 detail::readVolumeImpl(volume.traverser_begin(), shape_, s, buffer, vigra::MetaInt<2>());
476 volume.shape() == shape(),
"imported volume has wrong size");
480 for (
unsigned int i = 0; i < numbers_.size(); ++i)
483 std::string name = baseName_ + numbers_[i] + extension_;
486 ImageImportInfo info (name.c_str ());
489 MultiArrayView <2, T, Stride> view (volume.bindOuter (i));
490 vigra_precondition(view.shape() == info.shape(),
491 "importVolume(): the images have inconsistent sizes.");
499 VIGRA_EXPORT
void findImageSequence(
const std::string &name_base,
500 const std::string &name_ext,
501 std::vector<std::string> & numbers);
525 template <
class T,
class Allocator>
527 const std::string &name_base,
528 const std::string &name_ext)
533 info.importImpl(volume);
574 template <
class T,
class Allocator>
576 const std::string &filename)
581 info.importImpl(volume);
597 template <
class T,
class Str
ide>
600 info.importImpl(volume);
606 void setRangeMapping(std::string
const & pixeltype,
607 FindMinMax<T>
const & minmax, ImageExportInfo & info)
609 if(pixeltype ==
"UINT8")
610 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max,
611 (
double)NumericTraits<UInt8>::min(),
612 (
double)NumericTraits<UInt8>::max());
613 else if(pixeltype ==
"INT16")
614 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max,
615 (
double)NumericTraits<Int16>::min(),
616 (
double)NumericTraits<Int16>::max());
617 else if(pixeltype ==
"UINT16")
618 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max,
619 (
double)NumericTraits<UInt16>::min(),
620 (
double)NumericTraits<UInt16>::max());
621 else if(pixeltype ==
"INT32")
622 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max,
623 (
double)NumericTraits<Int32>::min(),
624 (
double)NumericTraits<Int32>::max());
625 else if(pixeltype ==
"UINT32")
626 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max,
627 (
double)NumericTraits<UInt32>::min(),
628 (
double)NumericTraits<UInt32>::max());
629 else if(pixeltype ==
"FLOAT")
630 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max, 0.0, 1.0);
631 else if(pixeltype ==
"DOUBLE")
632 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max, 0.0, 1.0);
635 template <
class T,
class Tag>
636 void setRangeMapping(MultiArrayView <3, T, Tag>
const & volume,
637 ImageExportInfo & info, VigraTrueType )
639 std::string pixeltype = info.getPixelType();
640 bool downcast = negotiatePixelType(getEncoderType(info.getFileName(), info.getFileType()),
641 TypeAsString<T>::result(), pixeltype);
645 FindMinMax<T> minmax;
647 setRangeMapping(pixeltype, minmax, info);
651 template <
class T,
class Tag>
652 void setRangeMapping(MultiArrayView <3, T, Tag>
const & volume,
653 ImageExportInfo & info, VigraFalseType )
655 typedef typename T::value_type SrcComponent;
656 std::string pixeltype = info.getPixelType();
657 bool downcast = negotiatePixelType(getEncoderType(info.getFileName(), info.getFileType()),
658 TypeAsString<SrcComponent>::result(), pixeltype);
662 unsigned int bands = volume(0,0,0).size();
663 FindMinMax<SrcComponent> minmax;
664 for(
unsigned int i=0; i<bands; ++i)
666 VectorComponentValueAccessor<T> band(i);
669 setRangeMapping(pixeltype, minmax, info);
695 template <
class T,
class Tag>
699 std::string name = std::string(volinfo.getFileNameBase()) + std::string(volinfo.getFileNameExt());
703 detail::setRangeMapping(volume, info,
typename NumericTraits<T>::isScalar());
705 const unsigned int depth = volume.
shape (2);
707 for (
unsigned int i = 0; i < depth; ++i)
711 std::stringstream stream;
712 stream << std::setfill (
'0') << std::setw (numlen) << i;
713 std::string name_num;
715 std::string name = std::string(volinfo.getFileNameBase()) + name_num + std::string(volinfo.getFileNameExt());
720 info.setFileName(name.c_str ());
726 template <
class T,
class Tag>
728 void exportVolume (MultiArrayView <3, T, Tag>
const & volume,
729 const std::string &name_base,
730 const std::string &name_ext)
732 VolumeExportInfo volinfo(name_base.c_str(), name_ext.c_str());
740 #endif // VIGRA_MULTI_IMPEX_HXX