Xmipp  v3.23.11-Nereus
rwEM.cpp
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 #include "xmipp_image_base.h"
27 #include "xmipp_error.h"
28 #include "xmipp_funcs.h"
29 #include "metadata_static.h"
30 
31 #define EMHEADERSIZE 512 // size of EM file header
32 
33 int ImageBase::readEM(size_t select_img, bool isStack)
34 {
35  // EM File formats does not support stacks
36  if (select_img > FIRST_IMAGE & !isStack)
37  REPORT_ERROR(ERR_ARG_INCORRECT, "readEM: EM file format does not support stacks.");
38 
39  EMHead header;
40 
41  if ( fread( &header, EMHEADERSIZE, 1, fimg ) != 1 )
42  REPORT_ERROR(ERR_IO_NOREAD, formatString("rwEM: cannot read EM main header from file %s"
43  ". Error message: %s", filename.c_str() ,strerror(errno)));
44 
45  // endian: If machine is SGI, OS-9 or MAC: Big Endian, otherwise Litle Endian
46  // Check Machine endianess
47  bool isLE = IsLittleEndian();
48 
49  if (header.machine == 0 || header.machine == 3 || header.machine == 5)
50  swap = isLE;
51  else if (header.machine == 1 || header.machine == 2 || header.machine == 4 || header.machine == 6)
52  swap = !isLE;
53  else
54  REPORT_ERROR(ERR_IMG_UNKNOWN, "rwEM: Unknown source machine to determine Endianness");
55 
56  if (swap)
57  swapPage((char *) &header, EMHEADERSIZE - 256, DT_UInt); // EMHEADERSIZE - 256 is to exclude userdata from swapping
58 
59  // Setting image dimensions
60 
61  ArrayDim aDim;
62  if (isStack)
63  {
64  if ( select_img > header.zdim )
65  REPORT_ERROR(ERR_INDEX_OUTOFBOUNDS, formatString("readEM: %s Image number %lu exceeds stack size %lu", this->filename.c_str(), select_img, header.zdim));
66 
67  aDim.ndim = (select_img > ALL_IMAGES)? 1 : header.zdim;
68  aDim.zdim = 1;
69  }
70  else
71  {
72  aDim.ndim = 1;
73  aDim.zdim = header.zdim;
74  }
75 
76  aDim.xdim = header.xdim;
77  aDim.ydim= header.ydim;
78 
79  replaceNsize = aDim.ndim;
80  setDimensions(aDim);
81 
82 
84  switch (header.datatype)
85  {
86  case 1:
87  datatype = DT_SChar;
88  break;
89  case 2:
90  datatype = DT_Short;
91  break;
92  case 4:
93  datatype = DT_Int;
94  break;
95  case 3:
96  case 5:
97  datatype = DT_Float;
98  break;
99  case 8:
100  datatype = DT_CFloat;
101  break;
102  case 9:
103  datatype = DT_CDouble;
104  break;
105  default:
106  REPORT_ERROR(ERR_ARG_INCORRECT, formatString("readEM: Unknown datatype value: %c", header.datatype));
107  break;
108  }
109  MDMainHeader.setValue(MDL_DATATYPE,(int)datatype);
110 
112 
113  // If only header is read: return
114  if (dataMode==HEADER || (dataMode == _HEADER_ALL && aDim.ndim > 1)) // Stop reading if not necessary
115  return 0;
116 
117  size_t imgStart = IMG_INDEX(select_img);
118  size_t imgEnd = (select_img != ALL_IMAGES) ? imgStart + 1 : aDim.ndim;
119 
120  MD.clear();
121  for (size_t i = 0; i < imgEnd-imgStart; i++)
122  MD.push_back(std::unique_ptr<MDRowVec>(new MDRowVec(MDL::emptyHeaderVec())));
123 
124  /* As MRC does not support stacks, we use the geometry stored in the header
125  for any image when we simulate the file is a stack.*/
126  if (dataMode == _HEADER_ALL || dataMode == _DATA_ALL)
127  {
128  for ( size_t i = 0; i < imgEnd - imgStart; ++i )
129  {
130  MD[i]->setValue(MDL_SHIFT_X, 0.);
131  MD[i]->setValue(MDL_SHIFT_Y, 0.);
132  MD[i]->setValue(MDL_SHIFT_Z, 0.);
133  MD[i]->setValue(MDL_ORIGIN_X, 0.);
134  MD[i]->setValue(MDL_ORIGIN_Y, 0.);
135  MD[i]->setValue(MDL_ORIGIN_Z, 0.);
136  }
137  }
138 
139  if ( dataMode < DATA ) // Don't read the individual header and the data if not necessary
140  return 0;
141 
142  readData(fimg, select_img, datatype, 0);
143 
144  return(0);
145 }
146 
147 
148 int ImageBase::writeEM(size_t select_img, bool isStack, int mode)
149 {
150  REPORT_ERROR(ERR_IO_NOWRITE, "ERROR: writeEM is not implemented.");
151  return(-1);
152 }
Index out of bounds.
Definition: xmipp_error.h:132
#define EMHEADERSIZE
Definition: rwEM.cpp:31
int readEM(size_t select_img, bool isStack=false)
Definition: rwEM.cpp:33
size_t xdim
DataMode dataMode
bool IsLittleEndian(void)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
virtual void readData(FILE *fimg, size_t select_img, DataType datatype, size_t pad)=0
Couldn&#39;t write to file.
Definition: xmipp_error.h:140
void setValue(const MDObject &object) override
Shift for the image in the X axis (double)
void swapPage(char *page, size_t pageNrElements, DataType datatype, int swap=1)
std::vector< std::unique_ptr< MDRow > > MD
if read from file original image datatype, this is an struct defined in image
Unknown image type.
Definition: xmipp_error.h:130
auxiliary label to be used as an index (long)
#define i
int writeEM(size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE)
Definition: rwEM.cpp:148
size_t zdim
Origin for the image in the Y axis (double)
Incorrect argument received.
Definition: xmipp_error.h:113
virtual void setDimensions(int Xdim, int Ydim, int Zdim, size_t Ndim)=0
Couldn&#39;t read from file.
Definition: xmipp_error.h:139
static MDRowVec emptyHeaderVec()
DataType
void mode
MDRowVec MDMainHeader
size_t ndim
DataType datatype() const
#define ALL_IMAGES
String formatString(const char *format,...)
Shift for the image in the Z axis (double)
#define IMG_INDEX(select_img)
size_t ydim
Origin for the image in the Z axis (double)
#define FIRST_IMAGE
FileName filename
Shift for the image in the Y axis (double)
size_t replaceNsize