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;
289 VIGRA_EXPORT VolumeExportInfo &
setCompression(
const char * type);
290 VIGRA_EXPORT
const char * getCompression()
const;
303 VIGRA_EXPORT VolumeExportInfo &
setPixelType(
const char * );
316 VIGRA_EXPORT VolumeExportInfo & setForcedRangeMapping(
double fromMin,
double fromMax,
317 double toMin,
double toMax);
318 VIGRA_EXPORT
bool hasForcedRangeMapping()
const;
319 VIGRA_EXPORT
double getFromMin()
const;
320 VIGRA_EXPORT
double getFromMax()
const;
321 VIGRA_EXPORT
double getToMin()
const;
322 VIGRA_EXPORT
double getToMax()
const;
327 VIGRA_EXPORT
float getXResolution()
const;
332 VIGRA_EXPORT
float getYResolution()
const;
337 VIGRA_EXPORT
float getZResolution()
const;
375 float m_x_res, m_y_res, m_z_res;
377 std::string m_filetype, m_filename_base, m_filename_ext, m_pixeltype, m_comp;
380 double fromMin_, fromMax_, toMin_, toMax_;
385 template <
class DestIterator,
class Shape,
class T>
387 readVolumeImpl(DestIterator d, Shape
const & shape, std::ifstream & s,
ArrayVector<T> & buffer, MetaInt<0>)
389 s.read((
char*)buffer.
begin(), shape[0]*
sizeof(T));
391 DestIterator dend = d + shape[0];
393 for(; d < dend; ++d, k++)
399 template <
class DestIterator,
class Shape,
class T,
int N>
401 readVolumeImpl(DestIterator d, Shape
const & shape, std::ifstream & s, ArrayVector<T> & buffer, MetaInt<N>)
403 DestIterator dend = d + shape[N];
406 readVolumeImpl(d.begin(), shape, s, buffer, MetaInt<N-1>());
412 template <
class T,
class Str
ide>
413 void VolumeImportInfo::importImpl(MultiArrayView <3, T, Stride> &volume)
const
415 vigra_precondition(this->shape() == volume.shape(),
"importVolume(): Volume must be shaped according to VolumeImportInfo.");
417 if(rawFilename_.size())
419 std::string dirName, baseName;
423 if(_getcwd(oldCWD, 2048) == 0)
426 vigra_fail(
"VolumeImportInfo: Unable to query current directory (getcwd).");
428 if(_chdir(path_.c_str()))
431 vigra_fail(
"VolumeImportInfo: Unable to change to new directory (chdir).");
434 if(getcwd(oldCWD, 2048) == 0)
437 vigra_fail(
"VolumeImportInfo: Unable to query current directory (getcwd).");
439 if(chdir(path_.c_str()))
442 vigra_fail(
"VolumeImportInfo: Unable to change to new directory (chdir).");
446 std::ifstream s(rawFilename_.c_str(), std::ios::binary);
447 vigra_precondition(s.good(),
"RAW file could not be opened");
449 ArrayVector<T> buffer(shape_[0]);
450 detail::readVolumeImpl(volume.traverser_begin(), shape_, s, buffer, vigra::MetaInt<2>());
464 volume.shape() == shape(),
"imported volume has wrong size");
468 for (
unsigned int i = 0; i < numbers_.size(); ++i)
471 std::string name = baseName_ + numbers_[i] + extension_;
474 ImageImportInfo info (name.c_str ());
477 MultiArrayView <2, T, Stride> view (volume.bindOuter (i));
478 vigra_precondition(view.shape() == info.shape(),
479 "importVolume(): the images have inconsistent sizes.");
487 VIGRA_EXPORT
void findImageSequence(
const std::string &name_base,
488 const std::string &name_ext,
489 std::vector<std::string> & numbers);
513 template <
class T,
class Allocator>
515 const std::string &name_base,
516 const std::string &name_ext)
521 info.importImpl(volume);
562 template <
class T,
class Allocator>
564 const std::string &filename)
569 info.importImpl(volume);
585 template <
class T,
class Str
ide>
588 info.importImpl(volume);
594 void setRangeMapping(std::string
const & pixeltype,
595 FindMinMax<T>
const & minmax, ImageExportInfo & info)
597 if(pixeltype ==
"UINT8")
598 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max,
601 else if(pixeltype ==
"INT16")
602 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max,
605 else if(pixeltype ==
"UINT16")
606 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max,
609 else if(pixeltype ==
"INT32")
610 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max,
613 else if(pixeltype ==
"UINT32")
614 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max,
617 else if(pixeltype ==
"FLOAT")
618 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max, 0.0, 1.0);
619 else if(pixeltype ==
"DOUBLE")
620 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max, 0.0, 1.0);
623 template <
class T,
class Tag>
624 void setRangeMapping(MultiArrayView <3, T, Tag>
const & volume,
625 ImageExportInfo & info, VigraTrueType )
627 std::string pixeltype = info.getPixelType();
628 bool downcast = negotiatePixelType(getEncoderType(info.getFileName(), info.getFileType()),
629 TypeAsString<T>::result(), pixeltype);
633 FindMinMax<T> minmax;
635 setRangeMapping(pixeltype, minmax, info);
639 template <
class T,
class Tag>
640 void setRangeMapping(MultiArrayView <3, T, Tag>
const & volume,
641 ImageExportInfo & info, VigraFalseType )
643 typedef typename T::value_type SrcComponent;
644 std::string pixeltype = info.getPixelType();
645 bool downcast = negotiatePixelType(getEncoderType(info.getFileName(), info.getFileType()),
646 TypeAsString<SrcComponent>::result(), pixeltype);
650 unsigned int bands = volume(0,0,0).size();
651 FindMinMax<SrcComponent> minmax;
652 for(
unsigned int i=0; i<bands; ++i)
654 VectorComponentValueAccessor<T> band(i);
657 setRangeMapping(pixeltype, minmax, info);
683 template <
class T,
class Tag>
687 std::string name = std::string(volinfo.getFileNameBase()) + std::string(volinfo.getFileNameExt());
691 detail::setRangeMapping(volume, info,
typename NumericTraits<T>::isScalar());
693 const unsigned int depth = volume.
shape (2);
695 for (
unsigned int i = 0; i < depth; ++i)
699 std::stringstream stream;
700 stream << std::setfill (
'0') << std::setw (numlen) << i;
701 std::string name_num;
703 std::string name = std::string(volinfo.getFileNameBase()) + name_num + std::string(volinfo.getFileNameExt());
708 info.setFileName(name.c_str ());
714 template <
class T,
class Tag>
716 void exportVolume (MultiArrayView <3, T, Tag>
const & volume,
717 const std::string &name_base,
718 const std::string &name_ext)
720 VolumeExportInfo volinfo(name_base.c_str(), name_ext.c_str());
728 #endif // VIGRA_MULTI_IMPEX_HXX