Xmipp  v3.23.11-Nereus
xmipp_image_generic.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Authors: Joaquin Oton (joton@cnb.csic.es)
3  *
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #ifndef CORE_IMAGE_GENERIC_H_
27 #define CORE_IMAGE_GENERIC_H_
28 
29 #include "xmipp_image_base.h"
30 #include "multidim_array_generic.h"
31 
32 // @addtogroup Images
34 
35 #define CHECK_IMG(op)\
36  if (image == NULL) \
37  REPORT_ERROR(ERR_IMG_UNKNOWN, "ImageGeneric: Image not initialized.");\
38  else\
39  {\
40  op\
41  }
42 
48 {
49 
50 public:
54 
55 protected:
56 
57  bool swap; // To store the swap mode of the image when the datatype is read.
58 
59 public:
60 
66  {
67  init();
68  }
73  ImageGeneric(DataType _datatype);
74 
79  ImageGeneric(const FileName &filename);
80 
82  ImageGeneric(const ImageGeneric &img);
83 
86  ~ImageGeneric();
87 
90  void init();
91 
94  void clear();
95 
97  void copy(const ImageGeneric &img);
98 
101  void clearHeader()
102  {
103  image->clearHeader();
104  }
105 
108  void initGeometry(const size_t n = 0);
111  MDRow& getGeometry(const size_t n = 0)
112  {
113  return *(image->MD[n]);
114  }
115 
118  void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const;
119  void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim) const;
120  void getDimensions(ArrayDim &aDim) const;
121 
124  size_t getSize() const
125  {
126  return NZYXSIZE(*(data->im));
127  }
128 
131  void getEulerAngles(double &rot, double &tilt, double &psi,
132  size_t n = 0)
133  {
134  image->getEulerAngles(rot, tilt, psi, n);
135  }
136 
139  double tilt(const size_t n = 0) const
140  {
141  return image->tilt(n);
142  }
143 
146  void resize(int Xdim, int Ydim, int Zdim, size_t Ndim, bool copy=true)
147  {
148  image->setDimensions(Xdim, Ydim, Zdim, Ndim);
149  data->resize(Ndim, Zdim, Ydim, Xdim, copy);
150  }
151 
154  void setDatatype(DataType _datatype);
155 
159  void setDatatype(const FileName &name, ImageInfo &imgInf);
160  void setDatatype(const FileName &name);
161 
164  {
165  image->setDataMode(mode);
166  }
167 
171  {
172  return datatype;
173  }
174 
177  void getInfo(ImageInfo &imgInfo) const;
178 
181  int getDatatypeDepth()const
182  {
183  switch (datatype)
184  {
185  case DT_Float:
186  case DT_UInt:
187  return 32;
188  case DT_Int:
189  return 31;
190  case DT_Short:
191  return 15;
192  case DT_UShort:
193  case DT_HalfFloat:
194  return 16;
195  case DT_SChar:
196  return 7;
197  case DT_UHalfByte:
198  case DT_UChar:
199  return 8;
200  default:
201  REPORT_ERROR(ERR_TYPE_INCORRECT,"Do not know how to handle this type at this point");
202  }
203  }
204 
207  inline bool isMapped()
208  {
209  return image->isMapped();
210  }
211 
214  int read(const FileName &name, DataMode datamode = DATA, size_t select_img = ALL_IMAGES,
215  bool mapData = false);
216 
219  int readApplyGeo(const FileName &name, const MDRow &row,
220  const ApplyGeoParams &params = DefaultApplyGeoParams);
221 
224  int readApplyGeo(const FileName &name, const MetaData &md, size_t objId,
225  const ApplyGeoParams &params = DefaultApplyGeoParams);
226 
228  int readApplyGeo(const MetaData &md, size_t objId,
229  const ApplyGeoParams &params = DefaultApplyGeoParams);
230 
232  void applyGeo(const MetaData &md, size_t objId,
233  const ApplyGeoParams &params = DefaultApplyGeoParams);
234  /* Euler mirror Y ---------------------------------------------------------- */
235  void mirrorY(void);
236 
239  int readMapped(const FileName &name, size_t select_img = ALL_IMAGES, int mode = WRITE_READONLY);
240 
241  /* Initially try to read normally, but if there is a memory allocation problem, then
242  * try to read from the mapped file.*/
243  int readOrReadMapped(const FileName &name, size_t select_img = ALL_IMAGES, int mode = WRITE_READONLY);
244 
245  /* Read an image with a lower resolution as a preview image.
246  * If Zdim parameter is not passed, then all slices are rescaled.
247  * If Ydim is not passed, then Ydim is rescaled same factor as Xdim.
248  */
249  int readPreview(const FileName &name, size_t Xdim, size_t Ydim = 0, int select_slice = CENTRAL_SLICE, size_t select_img = FIRST_IMAGE);
250 
256  int readOrReadPreview(const FileName &name, size_t Xdim, size_t Ydim = 0, int select_slice = CENTRAL_SLICE, size_t select_img = FIRST_IMAGE,
257  bool mapData = false, bool wrap = true);
258 
263  void getPreview(ImageGeneric &imgOut, int Xdim, int Ydim = -1, int select_slice = CENTRAL_SLICE, size_t select_img = FIRST_IMAGE);
264 
265  /* Read an image with a lower resolution as a preview image.
266  * Resizing is done in fourier space
267  * If Zdim parameter is not passed, then all slices are rescaled.
268  * If Ydim is not passed, then Ydim is rescaled same factor as Xdim.
269  */
270  int readPreviewFourier(const FileName &name, size_t Xdim, size_t Ydim = 0, int select_slice = CENTRAL_SLICE, size_t select_img = FIRST_IMAGE);
271 
272  /* Using xvsmooth */
273  int readPreviewSmooth(const FileName &name, size_t Xdim, size_t Ydim = 0, int select_slice = CENTRAL_SLICE, size_t select_img = FIRST_IMAGE);
274 
275 
278  inline void write(const FileName &name="", size_t select_img = ALL_IMAGES, bool isStack=false,
279  int mode=WRITE_OVERWRITE, CastWriteMode castMode = CW_CAST, int _swapWrite = 0)
280  {
281  image->write(name,select_img,isStack,mode,castMode,_swapWrite);
282  }
283 
284  /* Create an empty image file of format given by filename and map it to memory.
285  */
286  void mapFile2Write(int Xdim, int Ydim, int Zdim, const FileName &_filename,
287  bool createTempFile=false, size_t select_img = APPEND_IMAGE,
288  bool isStack=false, int mode=WRITE_OVERWRITE, int _swapWrite = 0);
289 
312  void movePointerTo(int select_slice = ALL_SLICES, size_t select_img = ALL_IMAGES)
313  {
314  image->movePointerTo(select_slice, select_img);
315  }
316 
317 
318  /* MultidimArrayGeneric data access
319  */
321  {
322  return *data;
323  }
324 
325  inline const MultidimArrayGeneric& operator()() const
326  {
327  return *data;
328  }
329 
333  ImageGeneric& operator=(const ImageGeneric &img);
334 
338  bool operator==(const ImageGeneric &i1) const;
339 
342  void convert2Datatype(DataType datatype, CastWriteMode castMode=CW_CONVERT);
343 
344  /* Reslice a volume aligning any X or Y direction with Z axis.
345  */
346  void reslice(AxisView view);
347  /* The resliced volume is returned in out
348  */
349  void reslice(AxisView view, ImageGeneric &out);
350 
354  bool equal(const ImageGeneric &i1, double accuracy=XMIPP_EQUAL_ACCURACY) const
355  {
356  return data->equal(MULTIDIM_ARRAY_GENERIC(i1),accuracy);
357  }
360  inline double getPixel(unsigned long n, int k, int i, int j) const
361  {
362  double ret;
363 #define GETVALUE(type) ret = NZYX_ELEM(*(MultidimArray<type>*)data->im,n,k,i,j);
364  SWITCHDATATYPE(datatype,GETVALUE)
365 #undef GETVALUE
366  return ret;
367  }
368 
371  inline double getPixel(int i, int j) const
372  {
373  double ret;
374 #define GETVALUE(type) ret = (double) A2D_ELEM(*(MultidimArray<type>*)data->im,i,j);
375  SWITCHDATATYPE(datatype,GETVALUE)
376 #undef GETVALUE
377  return ret;
378  }
379 
382  inline void setPixel(unsigned long n, int k, int i, int j, double value) const
383  {
384 #define SETVALUE(type) NZYX_ELEM(*(MultidimArray<type>*)data->im,n,k,i,j) = (type) value;
385  SWITCHDATATYPE(datatype,SETVALUE)
386 #undef SETVALUE
387 
388  }
389 
392  inline void setPixel(int i, int j, double value) const
393  {
394 
395 #define SETVALUE(type) A2D_ELEM(*(MultidimArray<type>*)data->im,i,j) = (type) value;
397 #undef SETVALUE
398 
399  }
400 
403  inline void initConstant(double value) const
404  {
405 #define INITCONS(type) (*(MultidimArray<type>*)(data->im)).initConstant((type) value);
407 #undef INITCONS
408 
409  }
410 
413  inline void initRandom(double op1, double op2, RandomMode mode = RND_UNIFORM) const
414  {
415 #define INITRND(type) (*(MultidimArray<type>*)(data->im)).initRandom(op1, op2, mode);
416  CHECK_IMG(SWITCHDATATYPE(datatype,INITRND))
417 #undef INITRND
418 
419  }
420 
423  void print() const;
424 
427  void toString(String &s) const;
428 
429  friend std::ostream& operator<<(std::ostream& o, const ImageGeneric& I)
430  {
431  o << I.image;
432  return o;
433  }
434 
437  void add(const ImageGeneric &img);
438 
441  void subtract(const ImageGeneric &img);
442 
445  void multiply(const double value);
446 
449  void multiply(const ImageGeneric &img);
450 
453  void divide(const ImageGeneric &img);
454 
457  void divide(const double value);
458 
459 }
460 ;
461 
462 
463 
465 
473 void createEmptyFile(const FileName &_filename, int Xdim, int Ydim, int Zdim = 1,
474  size_t select_img = APPEND_IMAGE, bool isStack = false,
475  int mode = WRITE_OVERWRITE, int _swapWrite = 0, const MDRowVec *md = nullptr);
476 
477 #endif /* IMAGE_GENERIC_H_ */
bool operator==(const ImageGeneric &i1) const
void setDataMode(DataMode mode)
void applyGeo(const MetaData &md, size_t objId, const ApplyGeoParams &params=DefaultApplyGeoParams)
void movePointerTo(int select_slice=ALL_SLICES, size_t select_img=ALL_IMAGES)
int readPreviewFourier(const FileName &name, size_t Xdim, size_t Ydim=0, int select_slice=CENTRAL_SLICE, size_t select_img=FIRST_IMAGE)
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)
void resize(int Xdim, int Ydim, int Zdim, size_t Ndim, bool copy=true)
void setPixel(unsigned long n, int k, int i, int j, double value) const
#define CHECK_IMG(op)
MDRow & getGeometry(const size_t n=0)
void toString(String &s) const
void initConstant(double value) const
friend std::ostream & operator<<(std::ostream &o, const ImageGeneric &I)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void add(const ImageGeneric &img)
void copy(const ImageGeneric &img)
int readPreviewSmooth(const FileName &name, size_t Xdim, size_t Ydim=0, int select_slice=CENTRAL_SLICE, size_t select_img=FIRST_IMAGE)
void getEulerAngles(double &rot, double &tilt, double &psi, size_t n=0)
#define INITRND(type)
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
MultidimArrayGeneric & operator()()
void multiply(const double value)
RandomMode
void createEmptyFile(const FileName &_filename, int Xdim, int Ydim, int Zdim=1, size_t select_img=APPEND_IMAGE, bool isStack=false, int mode=WRITE_OVERWRITE, int _swapWrite=0, const MDRowVec *md=nullptr)
#define APPEND_IMAGE
void initGeometry(const size_t n=0)
DataMode
#define i
#define GETVALUE(type)
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
DataType getDatatype() const
#define SETVALUE(type)
double tilt(const size_t n=0) const
const MultidimArrayGeneric & operator()() const
const ApplyGeoParams DefaultApplyGeoParams
AxisView
Definition: axis_view.h:32
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const
void getPreview(ImageGeneric &imgOut, int Xdim, int Ydim=-1, int select_slice=CENTRAL_SLICE, size_t select_img=FIRST_IMAGE)
void divide(const ImageGeneric &img)
void resize(size_t Ndim, int Zdim, int Ydim, int Xdim, bool copy=true)
#define XMIPP_EQUAL_ACCURACY
Definition: xmipp_macros.h:119
void getInfo(ImageInfo &imgInfo) const
bool equal(const MultidimArrayGeneric &op, double accuracy=XMIPP_EQUAL_ACCURACY) const
virtual void setDimensions(int Xdim, int Ydim, int Zdim, size_t Ndim)=0
void getEulerAngles(double &rot, double &tilt, double &psi, const size_t n=0) const
void mapFile2Write(int Xdim, int Ydim, int Zdim, const FileName &_filename, bool createTempFile=false, size_t select_img=APPEND_IMAGE, bool isStack=false, int mode=WRITE_OVERWRITE, int _swapWrite=0)
ImageGeneric & operator=(const ImageGeneric &img)
#define NZYXSIZE(v)
DataType
#define INITCONS(type)
int readOrReadPreview(const FileName &name, size_t Xdim, size_t Ydim=0, int select_slice=CENTRAL_SLICE, size_t select_img=FIRST_IMAGE, bool mapData=false, bool wrap=true)
void mode
Image base class.
double getPixel(unsigned long n, int k, int i, int j) const
void setDataMode(DataMode mode)
ImageBase * image
void reslice(AxisView view)
virtual void movePointerTo(int select_slice=ALL_SLICES, size_t select_img=ALL_IMAGES)=0
#define j
#define CENTRAL_SLICE
void convert2Datatype(DataType datatype, CastWriteMode castMode=CW_CONVERT)
#define MULTIDIM_ARRAY_GENERIC(v)
void setDatatype(DataType _datatype)
bool equal(const ImageGeneric &i1, double accuracy=XMIPP_EQUAL_ACCURACY) const
void initRandom(double op1, double op2, RandomMode mode=RND_UNIFORM) const
int readApplyGeo(const FileName &name, const MDRow &row, const ApplyGeoParams &params=DefaultApplyGeoParams)
std::string String
Definition: xmipp_strings.h:34
double psi(const double x)
#define ALL_IMAGES
int readOrReadMapped(const FileName &name, size_t select_img=ALL_IMAGES, int mode=WRITE_READONLY)
int getDatatypeDepth() const
#define FIRST_IMAGE
int readPreview(const FileName &name, size_t Xdim, size_t Ydim=0, int select_slice=CENTRAL_SLICE, size_t select_img=FIRST_IMAGE)
CastWriteMode
double getPixel(int i, int j) const
Incorrect type received.
Definition: xmipp_error.h:190
void setPixel(int i, int j, double value) const
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false)
MultidimArrayGeneric * data
#define ALL_SLICES
int readMapped(const FileName &name, size_t select_img=ALL_IMAGES, int mode=WRITE_READONLY)
int * n
void subtract(const ImageGeneric &img)
#define SWITCHDATATYPE(datatype, OP)
size_t getSize() const
double tilt(const size_t n=0) const