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"
52 #include "sifImport.hxx"
79 typedef ImageImportInfo::PixelType PixelType;
133 VIGRA_EXPORT
VolumeImportInfo(
const std::string &base_name,
const std::string &name_extension);
214 VIGRA_EXPORT PixelType
pixelType()
const;
217 VIGRA_EXPORT
bool isGrayscale()
const;
218 VIGRA_EXPORT
bool isColor()
const;
221 VIGRA_EXPORT
const std::string &name()
const;
223 VIGRA_EXPORT
const std::string &description()
const;
225 template <
class T,
class Str
ide>
229 void getVolumeInfoFromFirstSlice(
const std::string &filename);
236 std::string path_, name_, description_, fileType_, pixelType_;
238 std::string rawFilename_;
239 std::string baseName_, extension_;
240 std::vector<std::string> numbers_;
287 VIGRA_EXPORT
VolumeExportInfo(
const char * name_base,
const char * name_ext );
288 VIGRA_EXPORT ~VolumeExportInfo();
293 VIGRA_EXPORT VolumeExportInfo &
setFileNameBase(
const char * name_base);
305 VIGRA_EXPORT VolumeExportInfo &
setFileNameExt(
const char * name_ext);
306 VIGRA_EXPORT
const char * getFileNameBase()
const;
307 VIGRA_EXPORT
const char * getFileNameExt()
const;
359 VIGRA_EXPORT VolumeExportInfo &
setFileType(
const char * );
360 VIGRA_EXPORT
const char * getFileType()
const;
366 VIGRA_EXPORT VolumeExportInfo &
setCompression(
const char * type);
367 VIGRA_EXPORT
const char * getCompression()
const;
380 VIGRA_EXPORT VolumeExportInfo &
setPixelType(
const char * );
393 VIGRA_EXPORT VolumeExportInfo & setForcedRangeMapping(
double fromMin,
double fromMax,
394 double toMin,
double toMax);
395 VIGRA_EXPORT
bool hasForcedRangeMapping()
const;
396 VIGRA_EXPORT
double getFromMin()
const;
397 VIGRA_EXPORT
double getFromMax()
const;
398 VIGRA_EXPORT
double getToMin()
const;
399 VIGRA_EXPORT
double getToMax()
const;
404 VIGRA_EXPORT
float getXResolution()
const;
409 VIGRA_EXPORT
float getYResolution()
const;
414 VIGRA_EXPORT
float getZResolution()
const;
452 float m_x_res, m_y_res, m_z_res;
454 std::string m_filetype, m_filename_base, m_filename_ext, m_pixeltype, m_comp;
457 double fromMin_, fromMax_, toMin_, toMax_;
462 template <
class DestIterator,
class Shape,
class T>
464 readVolumeImpl(DestIterator d, Shape
const & shape, std::ifstream & s,
ArrayVector<T> & buffer, MetaInt<0>)
466 s.read(reinterpret_cast<char*>(buffer.
begin()), shape[0]*
sizeof(T));
468 DestIterator dend = d + shape[0];
470 for(; d < dend; ++d, k++)
476 template <
class DestIterator,
class Shape,
class T,
int N>
478 readVolumeImpl(DestIterator d, Shape
const & shape, std::ifstream & s, ArrayVector<T> & buffer, MetaInt<N>)
480 DestIterator dend = d + shape[N];
483 readVolumeImpl(d.begin(), shape, s, buffer, MetaInt<N-1>());
489 template <
class T,
class Str
ide>
490 void VolumeImportInfo::importImpl(MultiArrayView <3, T, Stride> &volume)
const
492 vigra_precondition(this->
shape() == volume.shape(),
"importVolume(): Output array must be shaped according to VolumeImportInfo.");
494 if(fileType_ ==
"RAW")
496 std::string dirName, baseName;
500 if(_getcwd(oldCWD, 2048) == 0)
503 vigra_fail(
"VolumeImportInfo: Unable to query current directory (getcwd).");
505 if(_chdir(path_.c_str()))
508 vigra_fail(
"VolumeImportInfo: Unable to change to new directory (chdir).");
511 if(getcwd(oldCWD, 2048) == 0)
514 vigra_fail(
"VolumeImportInfo: Unable to query current directory (getcwd).");
516 if(chdir(path_.c_str()))
519 vigra_fail(
"VolumeImportInfo: Unable to change to new directory (chdir).");
523 std::ifstream s(rawFilename_.c_str(), std::ios::binary);
524 vigra_precondition(s.good(),
"RAW file could not be opened");
526 ArrayVector<T> buffer(shape_[0]);
527 detail::readVolumeImpl(volume.traverser_begin(), shape_, s, buffer, vigra::MetaInt<2>());
541 volume.shape() ==
shape(),
"imported volume has wrong size");
543 else if(fileType_ ==
"STACK")
545 for (
unsigned int i = 0; i < numbers_.size(); ++i)
548 std::string filename = baseName_ + numbers_[i] + extension_;
551 ImageImportInfo info (filename.c_str ());
554 MultiArrayView <2, T, Stride> view (volume.bindOuter (i));
555 vigra_precondition(view.shape() == info.shape(),
556 "importVolume(): the images have inconsistent sizes.");
561 else if(fileType_ ==
"MULTIPAGE")
563 ImageImportInfo info(baseName_.c_str());
565 for(
int k=0; k<info.numImages(); ++k)
567 info.setImageIndex(k);
576 else if(fileType_ ==
"SIF")
578 SIFImportInfo infoSIF(baseName_.c_str());
584 VIGRA_EXPORT
void findImageSequence(
const std::string &name_base,
585 const std::string &name_ext,
586 std::vector<std::string> & numbers);
667 template <
class T,
class Str
ide>
670 MultiArrayView <3, T, Stride> &volume)
672 info.importImpl(volume);
675 template <
class T,
class Allocator>
678 const std::string &filename)
680 VolumeImportInfo info(filename);
681 volume.reshape(info.shape());
683 info.importImpl(volume);
686 template <
class T,
class Allocator>
687 void importVolume (MultiArray <3, T, Allocator> & volume,
688 const std::string &name_base,
689 const std::string &name_ext)
691 VolumeImportInfo info(name_base, name_ext);
692 volume.reshape(info.shape());
694 info.importImpl(volume);
700 void setRangeMapping(std::string
const & pixeltype,
701 FindMinMax<T>
const & minmax, ImageExportInfo & info)
703 if(pixeltype ==
"UINT8")
704 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max,
705 (
double)NumericTraits<UInt8>::min(),
706 (
double)NumericTraits<UInt8>::max());
707 else if(pixeltype ==
"INT16")
708 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max,
709 (
double)NumericTraits<Int16>::min(),
710 (
double)NumericTraits<Int16>::max());
711 else if(pixeltype ==
"UINT16")
712 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max,
713 (
double)NumericTraits<UInt16>::min(),
714 (
double)NumericTraits<UInt16>::max());
715 else if(pixeltype ==
"INT32")
716 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max,
717 (
double)NumericTraits<Int32>::min(),
718 (
double)NumericTraits<Int32>::max());
719 else if(pixeltype ==
"UINT32")
720 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max,
721 (
double)NumericTraits<UInt32>::min(),
722 (
double)NumericTraits<UInt32>::max());
723 else if(pixeltype ==
"FLOAT")
724 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max, 0.0, 1.0);
725 else if(pixeltype ==
"DOUBLE")
726 info.setForcedRangeMapping((
double)minmax.min, (
double)minmax.max, 0.0, 1.0);
729 template <
class T,
class Tag>
730 void setRangeMapping(MultiArrayView <3, T, Tag>
const & volume,
731 ImageExportInfo & info, VigraTrueType )
733 std::string pixeltype = info.getPixelType();
734 bool downcast = negotiatePixelType(getEncoderType(info.getFileName(), info.getFileType()),
735 TypeAsString<T>::result(), pixeltype);
739 FindMinMax<T> minmax;
741 setRangeMapping(pixeltype, minmax, info);
745 template <
class T,
class Tag>
746 void setRangeMapping(MultiArrayView <3, T, Tag>
const & volume,
747 ImageExportInfo & info, VigraFalseType )
749 typedef typename T::value_type SrcComponent;
750 std::string pixeltype = info.getPixelType();
751 bool downcast = negotiatePixelType(getEncoderType(info.getFileName(), info.getFileType()),
752 TypeAsString<SrcComponent>::result(), pixeltype);
756 unsigned int bands = volume(0,0,0).size();
757 FindMinMax<SrcComponent> minmax;
758 for(
unsigned int i=0; i<bands; ++i)
760 VectorComponentValueAccessor<T> band(i);
763 setRangeMapping(pixeltype, minmax, info);
829 template <
class T,
class Tag>
831 exportVolume (MultiArrayView <3, T, Tag>
const & volume,
832 const VolumeExportInfo & volinfo)
834 if(volinfo.getFileType() == std::string(
"MULTIPAGE"))
836 char const * mode =
"w";
837 std::string compression =
"LZW";
838 if(volinfo.getCompression() != std::string())
839 compression = volinfo.getCompression();
843 ImageExportInfo info(volinfo.getFileNameBase(), mode);
844 info.setFileType(
"TIFF");
845 info.setCompression(compression.c_str());
846 info.setPixelType(volinfo.getPixelType());
847 detail::setRangeMapping(volume, info,
typename NumericTraits<T>::isScalar());
854 std::string name = std::string(volinfo.getFileNameBase()) + std::string(volinfo.getFileNameExt());
855 ImageExportInfo info(name.c_str());
856 info.setCompression(volinfo.getCompression());
857 info.setPixelType(volinfo.getPixelType());
858 detail::setRangeMapping(volume, info,
typename NumericTraits<T>::isScalar());
860 const unsigned int depth = volume.shape (2);
862 for (
unsigned int i = 0; i < depth; ++i)
865 std::stringstream stream;
866 stream << std::setfill (
'0') << std::setw (numlen) << i;
867 std::string name_num;
869 std::string sliceFilename =
870 std::string(volinfo.getFileNameBase()) +
872 std::string(volinfo.getFileNameExt());
874 MultiArrayView <2, T, Tag> view (volume.bindOuter (i));
877 info.setFileName(sliceFilename.c_str ());
883 template <
class T,
class Tag>
885 exportVolume (MultiArrayView <3, T, Tag>
const & volume,
886 const std::string &filename)
888 VolumeExportInfo volinfo(filename.c_str());
892 template <
class T,
class Tag>
894 exportVolume (MultiArrayView <3, T, Tag>
const & volume,
895 const std::string &name_base,
896 const std::string &name_ext)
898 VolumeExportInfo volinfo(name_base.c_str(), name_ext.c_str());
906 #endif // VIGRA_MULTI_IMPEX_HXX
VolumeExportInfo & setZResolution(float)
VolumeExportInfo & setFileNameBase(const char *name_base)
void importVolume(...)
Function for importing a 3D volume.
VolumeExportInfo & setCompression(const char *type)
const_iterator begin() const
Definition: array_vector.hxx:223
void exportVolume(...)
Function for exporting a 3D volume.
void readSIF(const SIFImportInfo &info, MultiArrayView< 3, float > array)
Read the image data specified by the given vigra::SIFImportInfo object and write them into the given ...
Two dimensional difference vector.
Definition: diff2d.hxx:185
VolumeExportInfo & setPosition(const Diff2D &pos)
Diff2D getPosition() const
std::ptrdiff_t MultiArrayIndex
Definition: multi_fwd.hxx:60
const char * getFileType() const
const char * getPixelType() const
VolumeExportInfo & setYResolution(float)
void exportImage(...)
Write an image to a file.
const ICCProfile & getICCProfile() const
linalg::TemporaryMatrix< T > log10(MultiArrayView< 2, T, C > const &v)
Argument object for the function exportVolume().
Definition: multi_impex.hxx:257
TinyVector< float, 3 > Resolution
3D resolution type returned by resolution()
Definition: multi_impex.hxx:88
const char * getPixelType() const
Argument object for the function importVolume().
Definition: multi_impex.hxx:76
void importImage(...)
Read an image from a file.
doxygen_overloaded_function(template<...> void separableConvolveBlockwise) template< unsigned int N
Separated convolution on ChunkedArrays.
VolumeExportInfo & setPixelType(const char *)
MultiArrayIndex width() const
Class for fixed size vectors.This class contains an array of size SIZE of the specified VALUETYPE...
Definition: accessor.hxx:940
ArrayVector< unsigned char > ICCProfile
Definition: multi_impex.hxx:439
MultiArrayIndex depth() const
MultiArrayIndex height() const
image import and export functions
Base class for, and view to, vigra::MultiArray.
Definition: multi_array.hxx:704
MultiArrayShape< 3 >::type ShapeType
type of volume size returned by shape()
Definition: multi_impex.hxx:82
ShapeType size_type
type of volume size returned by shape()
Definition: multi_impex.hxx:85
int ceil(FixedPoint< IntBits, FracBits > v)
rounding up.
Definition: fixedpoint.hxx:675
VolumeExportInfo & setFileNameExt(const char *name_ext)
void inspectMultiArray(...)
Call an analyzing functor at every element of a multi-dimensional array.
Resolution resolution() const
PixelType pixelType() const
VolumeImportInfo(const std::string &filename)
VolumeExportInfo & setXResolution(float)
VolumeExportInfo & setICCProfile(const ICCProfile &profile)
VolumeExportInfo(const char *filename)
VolumeExportInfo & setFileType(const char *)