Xmipp  v3.23.11-Nereus
xmipp_image_extension.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_extension.h"
27 #include "xmipp_error.h"
28 #include "xmipp_image_generic.h"
29 
30 
31 void getImageSize(const FileName &filename, size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim)
32 {
33  Image<char> img;
34  img.read(filename, HEADER);
35  img.getDimensions(Xdim, Ydim, Zdim, Ndim);
36 }
37 
38 void getImageInfo(const FileName &filename, size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim, DataType &datatype)
39 {
40  Image<char> img;
41  img.read(filename, HEADER);
42  img.getDimensions(Xdim, Ydim, Zdim, Ndim);
43  datatype = img.datatype();
44 }
45 
46 void getImageInfo(const FileName &name, ImageInfo &imgInfo)
47 {
48  Image<char> image;
49  image.getInfo(name, imgInfo);
50 }
51 
52 /* Return the datatype of the image file.
53  */
54 void getImageDatatype(const FileName &name, DataType &datatype)
55 {
56  Image<char> image;
57  image.read(name, HEADER);
58  datatype = image.datatype();
59 }
61 {
62  Image<char> image;
63  image.read(name, HEADER);
64  return image.datatype();
65 }
66 
67 bool isImage(const FileName &name)
68 {
69  Image<char> I;
70  return I.isImage(name);
71 }
72 
73 bool checkImageFileSize(const FileName &name, const ImageInfo &imgInfo, bool error)
74 {
75  FileName ext = name.getExtension();
76  FileName dataFname;
77 
78  if ( ext.contains("hed"))
79  dataFname = name.removeLastExtension().addExtension("img");
80  else if (ext.contains("inf"))
81  dataFname = name.removeLastExtension();
82  else if (ext.contains("tif") || ext.contains("jpg") || ext.contains("hdf") || ext.contains("h5"))
83  return true;
84  else
85  dataFname = name;
86 
87  size_t explodeFactor = 1;
88 
89  if (imgInfo.datatype == DT_UHalfByte){
90  explodeFactor = 2;
91  }
92 
93 
94  size_t expectedSize = imgInfo.adim.nzyxdim*gettypesize(imgInfo.datatype) + imgInfo.offset;
95  size_t actualSize = dataFname.removeAllPrefixes().removeFileFormat().getFileSize();
96  actualSize *= explodeFactor;
97  bool result = (actualSize >= expectedSize);
98 
99  if (error && !result)
100  REPORT_ERROR(ERR_IO_SIZE, formatString("Image Extension: File %s has wrong size.\n"
101  "Expected size (at least) %u bytes. Actual size %u bytes.", name.c_str(), expectedSize, actualSize));
102 
103  return result;
104 }
105 
106 bool checkImageFileSize(const FileName &name, bool error)
107 {
108  ImageInfo imgInfo;
109  getImageInfo(name, imgInfo);
110 
111  return checkImageFileSize(name, imgInfo, error);
112 }
113 
114 bool checkImageCorners(const FileName &name)
115 {
116  const int windowSize=11;
117  const int windowSize_2=windowSize/2;
118  const double Flower=0.4871; // MATLAB: N=11*11-1; finv(0.00005,N,N)
119  // const double Fupper=2.0530; // MATLAB: N=11*11-1; finv(0.99995,N,N)
120 
121  ImageGeneric I;
122  I.readMapped(name);
123  size_t Xdim, Ydim, Zdim;
124  I.getDimensions(Xdim,Ydim,Zdim);
125  if (Zdim>1)
126  return true;
127  if (Xdim<=2*windowSize || Ydim<=2*windowSize)
128  return true;
129 
130  MultidimArray<double> window;
131  size_t i=Ydim/2;
132  size_t j=Xdim/2;
133  I().window(window,0,0,i-windowSize_2,j-windowSize_2,0,0,i+windowSize_2,j+windowSize_2);
134  double stddev0=window.computeStddev();
135  double var0=stddev0*stddev0;
136 
137  i=windowSize_2;
138  j=windowSize_2;
139  I().window(window,0,0,i-windowSize_2,j-windowSize_2,0,0,i+windowSize_2,j+windowSize_2);
140  double stddev1=window.computeStddev();
141  double var1=stddev1*stddev1;
142  double F=var1/var0;
143  if (F<Flower)//|| F>Fupper)
144  return false;
145 
146  i=Ydim-1-windowSize_2;
147  j=windowSize_2;
148  I().window(window,0,0,i-windowSize_2,j-windowSize_2,0,0,i+windowSize_2,j+windowSize_2);
149  stddev1=window.computeStddev();
150  var1=stddev1*stddev1;
151  F=var1/var0;
152  if (F<Flower)//|| F>Fupper)
153  return false;
154 
155  i=windowSize_2;
156  j=Xdim-1-windowSize_2;
157  I().window(window,0,0,i-windowSize_2,j-windowSize_2,0,0,i+windowSize_2,j+windowSize_2);
158  stddev1=window.computeStddev();
159  var1=stddev1*stddev1;
160  F=var1/var0;
161  if (F<Flower)//|| F>Fupper)
162  return false;
163 
164  i=Ydim-1-windowSize_2;
165  j=Xdim-1-windowSize_2;
166  I().window(window,0,0,i-windowSize_2,j-windowSize_2,0,0,i+windowSize_2,j+windowSize_2);
167  stddev1=window.computeStddev();
168  var1=stddev1*stddev1;
169  F=var1/var0;
170  if (F<Flower)//|| F>Fupper)
171  return false;
172 
173  return true;
174 }
175 
176 bool compareTwoImageTolerance(const FileName &fn1, const FileName &fn2, double tolerance, size_t index1, size_t index2)
177 {
178  double min_val, max_val, avg, stddev;
179  Image<double> img1;
180  Image<double> img2;
181  if (index1)
182  img1.read(fn1, DATA, index1);
183  else
184  img1.read(fn1);
185  if (index2)
186  img2.read(fn2, DATA, index2);
187  else
188  img2.read(fn2);
189  img1() -= img2();
190  img1().computeStats(avg, stddev, min_val, max_val);
191 
192  //return true if equal, false if different
193  return (fabs(avg) < tolerance && stddev < tolerance);
194 }
195 
FileName removeLastExtension() const
void getImageSize(const FileName &filename, size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
FileName removeFileFormat() const
FileName addExtension(const String &ext) const
#define i
String getExtension() const
FileName removeAllPrefixes() const
size_t nzyxdim
void getInfo(ImageInfo &imgInfo) const
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const
bool compareTwoImageTolerance(const FileName &fn1, const FileName &fn2, double tolerance, size_t index1, size_t index2)
binary comparison of two images with a tolerance factor
void getImageDatatype(const FileName &name, DataType &datatype)
void getImageInfo(const FileName &filename, size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim, DataType &datatype)
DataType
ArrayDim adim
bool contains(const String &str) const
#define j
DataType datatype() const
String formatString(const char *format,...)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
DataType datatype
Incorrect file size.
Definition: xmipp_error.h:145
double computeStddev() const
bool isImage(const FileName &name)
int readMapped(const FileName &name, size_t select_img=ALL_IMAGES, int mode=WRITE_READONLY)
bool checkImageFileSize(const FileName &name, const ImageInfo &imgInfo, bool error)
size_t getFileSize() const
bool isImage(const FileName &name)
bool checkImageCorners(const FileName &name)
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const
size_t gettypesize(DataType type)
Returns memory size of datatype.