73 MD.reserve(other.
MD.size());
74 if ((other.
MD.size() > 0) && (dynamic_cast<MDRowVec*>(other.
MD[0].get()) !=
nullptr)) {
75 for (
const std::unique_ptr<MDRow>& rowPtr : other.
MD) {
76 const MDRow& row = *rowPtr;
77 MD.emplace_back(std::unique_ptr<MDRow>(
new MDRowVec(dynamic_cast<const MDRowVec&>(row))));
80 if ((other.
MD.size() > 0) && (dynamic_cast<MDRowSql*>(other.
MD[0].get()) !=
nullptr)) {
81 for (
const std::unique_ptr<MDRow>& rowPtr : other.
MD) {
82 const MDRow& row = *rowPtr;
83 MD.emplace_back(std::unique_ptr<MDRow>(
new MDRowSql(dynamic_cast<const MDRowSql&>(row))));
120 bool mapData,
int mode)
126 int err =
_read(name,
hFile, datamode, select_img, mapData);
139 int err =
_readBatch(name,
hFile, start_img, batch_size, datamode, mapData);
147 if (end_img < start_img) {
151 readBatch(name, start_img, end_img - start_img + 1, datamode, mapData, mode);
159 bool swap = this->swap > 0;
160 return read(name,
DATA, select_img, !swap, mode);
167 return read(name,
DATA, select_img,
false, mode);
173 reportWarning(
"ImageBase::readOrReadMapped: Not enough memory to allocate. \n" 174 " Proceeding to map image from file.");
186 size_t imXdim, imYdim, imZdim, imNdim;
189 if (imXdim != Xdim || imYdim != Ydim)
190 return readPreview(name, Xdim, Ydim, select_slice, select_img);
193 int ret =
read(name,
DATA, select_img, mapData);
203 bool createTempFile,
size_t select_img,
bool isStack,
int mode)
240 #define READ_AND_RETURN() ImageFHandler* hFile = openFile(name); \ 241 int err = _read(name, hFile, params.datamode, params.select_img); \ 242 applyGeo(*row, params.only_apply_shifts, params.wrap); \ 248 std::unique_ptr<const MDRow> row(md.
getRow(objId));
255 if (dynamic_cast<const MDRowVec*>(&row) !=
nullptr)
256 MD[n] = std::unique_ptr<MDRow>(
new MDRowVec(dynamic_cast<const MDRowVec&>(row)));
257 if (dynamic_cast<const MDRowSql*>(&row) !=
nullptr)
258 MD[n] = std::unique_ptr<MDRow>(
new MDRowSql(dynamic_cast<const MDRowSql&>(row)));
277 std::unique_ptr<const MDRow> row(md.
getRow(objId));
285 std::unique_ptr<const MDRow> row(md.
getRow(objId));
307 if (hasTempFile && std::rename(
tempFilename.c_str(), fname.c_str()) != 0)
327 _write(fname,
hFile, select_img, isStack, mode, castMode);
336 std::cerr<<
"DEBUG swapPage: Swapping image data with swap= " 337 << swap<<
" datatypesize= "<<datatypesize
338 <<
" pageNrElements " << pageNrElements
339 <<
" datatype " << datatype
349 for (
size_t i = 0;
i < pageNrElements;
i += datatypesize )
354 for (
size_t i=0;
i<pageNrElements;
i+=
swap )
520 const size_t n)
const 586 size_t found = fileName.find_first_of(
"%");
587 if (found!=String::npos)
588 fileName = fileName.substr(0, found) ;
590 bool exist = fileName.
exists();
591 bool sizeZero =
true;
595 hFile->
exist = exist && !sizeZero;
615 wmChar = (hFile->
exist) ?
"r+" :
"w+";
622 TIFFSetWarningHandler(NULL);
623 if ((hFile->
tif = TIFFOpen(fileName.c_str(), wmChar.c_str())) == NULL)
630 if ((hFile->
fhdf5 = H5Fopen(fileName.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT)) == -1 )
634 if ( (hFile->
fimg = fopen(fileName.c_str(), wmChar.c_str())) == NULL )
673 if ( (hFile->
fimg = fopen(fileName.c_str(), wmChar.c_str())) == NULL )
684 if ((hFile->
fhed = fopen(headName.c_str(), wmChar.c_str())) == NULL )
721 fhdf5 = hFile->
fhdf5;
747 if (fclose(fimg) != 0 )
752 if (fclose(fimg) != 0 )
755 if (fhed != NULL && fclose(fhed) != 0 )
766 mrcExtensions=getenv(envvar);
768 size_t found = ext.find_first_of(
"%");
769 if (found!=String::npos)
770 plainExt = ext.substr(0, found);
773 return mrcExtensions.
contains(plainExt);
796 if (select_img == (
size_t) -1)
816 const auto &ext_name = hFile->
ext_name;
827 select_img = image_num;
833 std::cerr <<
"READ\n" <<
834 "name="<<name <<std::endl;
835 std::cerr <<
"ext= "<<ext_name <<std::endl;
837 <<
" select_img " << select_img <<
", image_num = " << image_num << std::endl;
848 fseek(
fimg, 0, SEEK_SET);
850 fseek(
fhed, 0, SEEK_SET);
852 if (ext_name.contains(
"spi") || ext_name.contains(
"xmp") ||
853 ext_name.contains(
"stk") || ext_name.contains(
"vol"))
856 err =
readMRC(select_img,
true);
858 err =
readMRC(select_img,
false);
859 else if (ext_name.contains(
"eer"))
861 else if (ext_name.contains(
"img") || ext_name.contains(
"hed"))
863 else if (ext_name.contains(
"ser"))
864 err =
readTIA(select_img,
false);
865 else if (ext_name.contains(
"dm3"))
866 err =
readDM3(select_img,
false);
867 else if (ext_name.contains(
"dm4"))
868 err =
readDM4(select_img,
false);
869 else if (ext_name.contains(
"ems"))
870 err =
readEM(select_img,
true);
871 else if (ext_name.contains(
"em"))
873 else if (ext_name.contains(
"pif"))
875 else if (ext_name.contains(
"inf"))
876 err =
readINF(select_img,
false);
877 else if (ext_name.contains(
"raw"))
878 err =
readRAW(select_img,
false);
879 else if (ext_name.contains(
"tif") || ext_name.contains(
"gain"))
881 else if (ext_name.contains(
"spe"))
882 err =
readSPE(select_img,
false);
883 else if (ext_name.contains(
"jpg"))
885 else if (ext_name.contains(
"hdf") || ext_name.contains(
"h5"))
915 const auto &ext_name = hFile->
ext_name;
929 fseek(
fimg, 0, SEEK_SET);
931 fseek(
fhed, 0, SEEK_SET);
933 if (ext_name.contains(
"spi") || ext_name.contains(
"xmp") ||
934 ext_name.contains(
"stk") || ext_name.contains(
"vol")) {
937 err =
readMRC(start_img, batch_size,
true);
939 err =
readMRC(start_img, batch_size,
false);
954 if (select_img == (
size_t) -1)
983 size_t found = filNamePlusExt.find_first_of(
"%");
985 if (found!=String::npos)
987 imParam = filNamePlusExt.substr(found+1).c_str();
988 filNamePlusExt = filNamePlusExt.substr(0, found) ;
994 std::cerr <<
"write" <<std::endl;
995 std::cerr<<
"extension for write= "<<ext_name<<std::endl;
996 std::cerr<<
"filename= "<<
filename<<std::endl;
997 std::cerr<<
"mode= "<<mode<<std::endl;
998 std::cerr<<
"isStack= "<<isStack<<std::endl;
999 std::cerr<<
"select_img= "<<select_img<<std::endl;
1000 std::cerr <<
"exists=" <<
_exists << std::endl;
1013 size_t Xdim, Ydim, Zdim, _Xdim, _Ydim, _Zdim, Ndim, _Ndim;
1014 Xdim = Ydim = Zdim = _Xdim = _Ydim = _Zdim = Ndim = _Ndim = 0;
1015 if (
nullptr == m_auxI) {
1037 "ImageBase::Write: images source and target have different sizes:\n" 1038 "Image source to be written (x,y,z,n) = %d %d %d %lu\n" 1039 "Image file target %s (x,y,z,n) = %d %d %d %lu",
1040 Xdim,Ydim,Zdim,Ndim,
dataFName.c_str(),_Xdim,_Ydim,_Zdim,_Ndim));
1058 fseek(
fimg, 0, SEEK_SET);
1060 fseek(
fhed, 0, SEEK_SET);
1068 writeMRC(select_img,isStack,mode,imParam,castMode);
1076 writeEM(select_img,
false,mode);
1082 writeINF(select_img,
false,mode,imParam,castMode);
1084 writeTIFF(select_img,isStack,mode,imParam,castMode);
1096 std::cerr <<
" Filename = " <<
filename <<
" Extension= " << ext_name << std::endl;
1127 o <<
"--- File information ---" << std::endl;
1128 o <<
"Filename : " << I.
filename << std::endl;
1129 o <<
"Endianess : ";
1131 o <<
"Little" << std::endl;
1133 o <<
"Big" << std::endl;
1137 o <<
"True" << std::endl;
1139 o <<
"False" << std::endl;
1143 o <<
"Data offset : " << I.
offset << std::endl;
1146 o <<
"--- Image information ---" << std::endl;
1149 if ((fileDT == NULL || myDT != *fileDT) && I.
dataMode >=
DATA )
1151 o <<
"Image type : ";
1153 o <<
"Fourier-space image" << std::endl;
1155 o <<
"Real-space image" << std::endl;
1159 size_t xdim, ydim, zdim, ndim;
1161 o <<
"Dimensions : " << ndim <<
" x " << zdim <<
" x " << ydim <<
" x " << xdim;
1162 o <<
" ((N)Objects x (Z)Slices x (Y)Rows x (X)Columns)" << std::endl;
1168 o <<
"Sampling rate : " << std::endl;
1169 o <<
" X-rate (Angstrom/pixel) = " << sampling << std::endl;
1171 o <<
" Y-rate (Angstrom/pixel) = " << sampling << std::endl;
1173 o <<
" Z-rate (Angstrom/pixel) = " << sampling << std::endl;
1176 std::stringstream oGeo;
1180 oGeo <<
"Euler angles : " << std::endl;
1181 oGeo <<
" Phi (rotation around Z axis) = " << I.
rot() << std::endl;
1182 oGeo <<
" Theta (tilt, second rotation around new Y axis) = " << I.
tilt() << std::endl;
1183 oGeo <<
" Psi (third rotation around new Z axis) = " << I.
psi() << std::endl;
1187 oGeo <<
"Origin Offsets : " << std::endl;
1188 oGeo <<
" Xoff (origin offset in X-direction) = " << I.
Xoff() << std::endl;
1189 oGeo <<
" Yoff (origin offset in Y-direction) = " << I.
Yoff() << std::endl;
1190 oGeo <<
" Zoff (origin offset in Z-direction) = " << I.
Zoff() << std::endl;
1193 oGeo <<
"Scale : " <<I.
scale() << std::endl;
1195 oGeo <<
"Weight : " << I.
weight() << std::endl;
1197 oGeo <<
"Flip : " << I.
flip() << std::endl;
1199 if (!oGeo.str().empty())
1200 o <<
"--- Geometry ---" << std::endl << oGeo.str();
virtual void munmapFile()=0
ImageFHandler * openFile(const FileName &name, int mode=WRITE_READONLY) const
int readBatch(const FileName &name, size_t start_img, size_t batch_size, DataMode datamode=DATA, bool mapData=false, int mode=WRITE_READONLY)
int readOrReadMapped(const FileName &name, size_t select_img=ALL_IMAGES, int mode=WRITE_READONLY)
int writeTIA(int img_select, bool isStack=false, int mode=WRITE_OVERWRITE)
std::array< int, 4 > axisOrder
int readEM(size_t select_img, bool isStack=false)
Case or algorithm not implemented yet.
void reportWarning(const String &what)
int writeDM3(size_t img_select, bool isStack=false, int mode=WRITE_OVERWRITE)
Just an error for debugging purpose.
bool IsLittleEndian(void)
#define REPORT_ERROR(nerr, ErrormMsg)
Error with some arguments dependencies.
FileName removeFileFormat() const
void swapbytes(char *v, unsigned long n)
double psi(const size_t n=0) const
double Zoff(const size_t n=0) const
int writeSPE(size_t select_img, bool isStack=false, int mode=WRITE_OVERWRITE)
int readRange(const FileName &name, size_t start_img, size_t end_img, DataMode datamode=DATA, bool mapData=false, int mode=WRITE_READONLY)
int readDM3(size_t img_select, bool isStack=false)
Couldn't write to file.
FileName addExtension(const String &ext) const
void setValue(const MDObject &object) override
int writeHDF5(size_t select_img, bool isStack=false, int mode=WRITE_OVERWRITE, String bitDepth="", CastWriteMode castMode=CW_CAST)
int readSPIDER(size_t select_img)
int writeIMAGIC(size_t img_select=ALL_IMAGES, int mode=WRITE_OVERWRITE, const String &bitDepth="", CastWriteMode castMode=CW_CAST)
There is not enough memory for allocation.
void swapPage(char *page, size_t pageNrElements, DataType datatype, int swap=1)
void write(const FileName &name="", size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST, int _swapWrite=0)
std::vector< std::unique_ptr< MDRow > > MD
static void emptifyHeader(MDRow &)
friend std::ostream & operator<<(std::ostream &o, const ImageBase &I)
const T & getValueOrDefault(MDLabel label, const T &def) const
int readSPE(size_t select_img, bool isStack=false)
void setDatatype(DataType datatype)
int readIMAGIC(size_t img_select)
const FileName & name() const
static constexpr std::array< int, 4 > defaultAxisOrder
virtual int readPreview(const FileName &name, size_t Xdim, size_t Ydim=0, int select_slice=CENTRAL_SLICE, size_t select_img=FIRST_IMAGE)=0
Input/Output general error.
void initUniqueName(const char *templateStr="xmippTemp_XXXXXX", const String &fnDir="")
Incorrect MultidimArray size.
double rot(const size_t n=0) const
void decompose(size_t &no, String &str) const
double weight(const size_t n=0) const
int readApplyGeo(const FileName &name, const MDRow &row, const ApplyGeoParams ¶ms=DefaultApplyGeoParams)
Incorrect number of objects in Metadata.
int writeTIFF(size_t select_img, bool isStack=false, int mode=WRITE_OVERWRITE, String bitDepth="", CastWriteMode castMode=CW_CAST)
T & getValue(MDLabel label)
int writeMRC(size_t select_img, bool isStack=false, int mode=WRITE_OVERWRITE, const String &bitDepth="", CastWriteMode castMode=CW_CAST)
void _write(const FileName &name, ImageFHandler *hFile, size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST)
double tilt(const size_t n=0) const
String getExtension() const
FileName removeAllPrefixes() const
int writeEM(size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE)
void closeFile(ImageFHandler *hFile=NULL) const
void setShifts(double xoff, double yoff, double zoff=0., const size_t n=0)
void getInfo(ImageInfo &imgInfo) const
int readTIA(int img_select, bool isStack=false)
int readMRC(size_t select_img, bool isStack=false)
int readPIF(size_t select_img)
int writeINF(size_t img_select, bool isStack=false, int mode=WRITE_OVERWRITE, String bitDepth="", CastWriteMode castMode=CW_CAST)
size_t getPrefixNumber(size_t pos=0) const
Incorrect argument received.
int readOrReadPreview(const FileName &name, size_t Xdim, size_t Ydim, int select_slice=CENTRAL_SLICE, size_t select_img=FIRST_IMAGE, bool mapData=false)
int writePIF(size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE)
virtual void setDimensions(int Xdim, int Ydim, int Zdim, size_t Ndim)=0
double Yoff(const size_t n=0) const
void getEulerAngles(double &rot, double &tilt, double &psi, const size_t n=0) const
double scale(const size_t n=0) const
virtual bool isComplexT() const =0
int readEER(size_t select_img)
File or directory does not exist.
void applyGeo(const MetaData &md, size_t objId, const ApplyGeoParams ¶ms=DefaultApplyGeoParams)
bool contains(const String &str) const
std::string datatype2StrLong(DataType datatype)
int writeSPIDER(size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE)
int readHDF5(size_t select_img)
void setDimensions(int Xdim, int Ydim, int Zdim, size_t Ndim)
virtual void movePointerTo(int select_slice=ALL_SLICES, size_t select_img=ALL_IMAGES)=0
bool isDynamicMRC(const char *envvar, const String &ext)
int readJPEG(size_t select_img)
int readTIFF(size_t select_img, bool isStack=false)
void setEulerAngles(double rot, double tilt, double psi, const size_t n=0)
int readINF(size_t img_select, bool isStack=false)
bool individualContainsLabel(MDLabel label) const
void initGeometry(const size_t n=0)
int _read(const FileName &name, ImageFHandler *hFile, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false)
int readDM4(size_t img_select, bool isStack=false)
double samplingRateX() const
FileName withoutExtension() const
int readRAW(size_t select_img, bool isStack=false)
DataType datatype() const
double Xoff(const size_t n=0) const
String formatString(const char *format,...)
String getFileFormat() const
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
bool flip(const size_t n=0) const
#define READ_AND_RETURN()
virtual DataType myT() const =0
bool isMRCStack(const FileName &ext_name)
Insufficient permissions to perform operation.
bool isImage(const FileName &name)
int readMapped(const FileName &name, size_t select_img=ALL_IMAGES, int mode=WRITE_READONLY)
Incorrect value received.
void copy(const ImageBase &other)
void mapFile2Write(size_t Xdim, size_t Ydim, size_t Zdim, const FileName &_filename, bool createTempFile=false, size_t select_img=APPEND_IMAGE, bool isStack=false, int mode=WRITE_OVERWRITE)
MultidimArrayBase * mdaBase
int writeJPEG(size_t select_img, bool isStack=false, int mode=WRITE_OVERWRITE, String bitDepth="", CastWriteMode castMode=CW_CONVERT)
size_t getFileSize() const
int writeDM4(size_t img_select, bool isStack=false, int mode=WRITE_OVERWRITE)
virtual size_t getSize() const =0
void getShifts(double &xoff, double &yoff, double &zoff, const size_t n=0) const
bool isMRCImageOrVolume(const FileName &ext_name)
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const
void setGeo(const MDRow &row, size_t n=0)
int _readBatch(const FileName &name, ImageFHandler *hFile, size_t start_img, size_t batch_size, DataMode datamode=DATA, bool mapData=false)
size_t gettypesize(DataType type)
Returns memory size of datatype.