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;
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_;
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_;
462template <
class DestIterator,
class Shape,
class T>
466 s.read(
reinterpret_cast<char*
>(
buffer.
begin()), shape[0]*
sizeof(T));
470 for(; d <
dend; ++d,
k++)
476template <
class DestIterator,
class Shape,
class T,
int N>
478readVolumeImpl(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>());
489template <
class T,
class Str
ide>
490void 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());
584VIGRA_EXPORT
void findImageSequence(
const std::string &name_base,
585 const std::string &name_ext,
586 std::vector<std::string> & numbers);
667template <
class T,
class Str
ide>
672 info.importImpl(volume);
675template <
class T,
class Allocator>
678 const std::string &filename)
680 VolumeImportInfo info(filename);
681 volume.reshape(info.shape());
683 info.importImpl(volume);
686template <
class T,
class Allocator>
687void 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);
700void 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);
729template <
class T,
class Tag>
730void 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);
745template <
class T,
class Tag>
746void 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);
829template <
class T,
class Tag>
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();
844 info.setFileType(
"TIFF");
845 info.setCompression(compression.c_str());
847 detail::setRangeMapping(volume,
info,
typename NumericTraits<T>::isScalar());
854 std::string name = std::string(
volinfo.getFileNameBase()) + std::string(
volinfo.getFileNameExt());
858 detail::setRangeMapping(volume,
info,
typename NumericTraits<T>::isScalar());
860 const unsigned int depth = volume.shape (2);
861 int numlen =
static_cast <int> (std::ceil (std::log10 ((
double)depth)));
862 for (
unsigned int i = 0;
i < depth; ++
i)
870 std::string(
volinfo.getFileNameBase()) +
872 std::string(
volinfo.getFileNameExt());
883template <
class T,
class Tag>
886 const std::string &filename)
888 VolumeExportInfo volinfo(filename.c_str());
892template <
class T,
class Tag>
895 const std::string &name_base,
896 const std::string &name_ext)
898 VolumeExportInfo volinfo(name_base.c_str(), name_ext.c_str());
Two dimensional difference vector.
Definition diff2d.hxx:186
Argument object for the function exportImage().
Definition imageinfo.hxx:134
Class for a single RGB value.
Definition rgbvalue.hxx:128
iterator begin()
Definition tinyvector.hxx:861
Class for fixed size vectors.
Definition tinyvector.hxx:1008
Argument object for the function exportVolume().
Definition multi_impex.hxx:258
VolumeExportInfo & setPosition(const Diff2D &pos)
VolumeExportInfo & setCompression(const char *type)
ArrayVector< unsigned char > ICCProfile
Definition multi_impex.hxx:439
const char * getPixelType() const
VolumeExportInfo & setYResolution(float)
VolumeExportInfo(const char *filename)
Diff2D getPosition() const
const ICCProfile & getICCProfile() const
VolumeExportInfo & setFileType(const char *)
VolumeExportInfo & setZResolution(float)
VolumeExportInfo & setXResolution(float)
VolumeExportInfo & setFileNameBase(const char *name_base)
VolumeExportInfo(const char *name_base, const char *name_ext)
VolumeExportInfo & setICCProfile(const ICCProfile &profile)
VolumeExportInfo & setPixelType(const char *)
VolumeExportInfo & setFileNameExt(const char *name_ext)
Argument object for the function importVolume().
Definition multi_impex.hxx:77
MultiArrayShape< 3 >::type ShapeType
type of volume size returned by shape()
Definition multi_impex.hxx:82
MultiArrayIndex height() const
const char * getPixelType() const
MultiArrayIndex width() const
ShapeType size_type
type of volume size returned by shape()
Definition multi_impex.hxx:85
TinyVector< float, 3 > Resolution
3D resolution type returned by resolution()
Definition multi_impex.hxx:88
VolumeImportInfo(const std::string &base_name, const std::string &name_extension)
MultiArrayIndex depth() const
PixelType pixelType() const
const char * getFileType() const
Resolution resolution() const
VolumeImportInfo(const std::string &filename)
void exportImage(...)
Write an image to a file.
void importImage(...)
Read an image from a file.
image import and export functions
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 ...
void inspectMultiArray(...)
Call an analyzing functor at every element of a multi-dimensional array.
void importVolume(...)
Function for importing a 3D volume.
std::ptrdiff_t MultiArrayIndex
Definition multi_fwd.hxx:60
void exportVolume(...)
Function for exporting a 3D volume.