00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef OLENA_LRDE_EFIGI_IO_HH
00029 # define OLENA_LRDE_EFIGI_IO_HH
00030
00031 #include <cmath>
00032
00033 #include "fitsio.h"
00034
00035 #include <iostream>
00036 #include <fstream>
00037 #include <string>
00038
00039 #include <oln/basics2d.hh>
00040
00041
00042
00043 namespace oln
00044 {
00045
00046 namespace efigi
00047 {
00048
00049
00050
00051
00052 image2d<float> load_fits(const std::string& filename);
00053
00054 image2d<float> load_pfm(const std::string& filename);
00055 void save_pfm(const image2d<float>& ima, const std::string& filename);
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 void fits_exit(int status)
00097 {
00098 if (status)
00099 {
00100 fits_report_error(stderr, status);
00101 exit(status);
00102 }
00103 return;
00104 }
00105
00106
00107 image2d<float> load_fits(const char* filename)
00108 {
00109 fitsfile *fptr;
00110 int status, nfound, anynull;
00111 long naxes[2];
00112 float nullval;
00113
00114 status = 0;
00115 if (fits_open_file(&fptr, filename, READONLY, &status))
00116 fits_exit(status);
00117
00118 if (fits_read_keys_lng(fptr, "NAXIS", 1, 2, naxes, &nfound, &status))
00119 fits_exit(status);
00120
00121 const int nrows = naxes[0], ncols = naxes[1];
00122 std::cout << "nrows = " << nrows << " ncols = " << ncols << std::endl;
00123 image2d<float> output(nrows, ncols);
00124
00125 nullval = 0;
00126
00127 for (int row = 0; row < nrows; ++row)
00128 {
00129 if (fits_read_img(fptr,
00130 TFLOAT,
00131 1 + row * ncols,
00132 ncols,
00133 &nullval,
00134 &(output(row, 0)),
00135 &anynull,
00136 &status))
00137 fits_exit(status);
00138 }
00139
00140 if (fits_close_file(fptr, &status))
00141 fits_exit(status);
00142
00143 return output;
00144 }
00145
00146
00147 void save_pfm(const image2d<float>& ima,
00148 const std::string& filename)
00149 {
00150 std::ofstream file(filename.c_str());
00151 file << ima.nrows() << ' ' << ima.ncols() << std::endl
00152 << "float" << std::endl;
00153
00154 image2d<float>& ima_ = const_cast< image2d<float>& >(ima);
00155 for (int row = 0; row < ima.nrows(); ++row)
00156 file.write((const char*)(&(ima_(row, 0))),
00157 sizeof(float) * ima.ncols());
00158 file.close();
00159 }
00160
00161
00162 image2d<float> load_pfm(const std::string& filename)
00163 {
00164 std::ifstream file(filename.c_str());
00165 int nrows, ncols;
00166 file >> nrows;
00167 file >> ncols;
00168 if (file.get() != '\n')
00169 {
00170 std::cerr << "pb in 'oln::efigi::load_pfm' with "
00171 << filename << std::endl;
00172 exit(EXIT_FAILURE);
00173 }
00174 std::string tag;
00175 file >> tag;
00176 if (tag != "float")
00177 {
00178 std::cerr << "pb in 'oln::efigi::load_pfm' with "
00179 << filename << std::endl;
00180 exit(EXIT_FAILURE);
00181 }
00182 if (file.get() != '\n')
00183 {
00184 std::cerr << "pb in 'oln::efigi::load_pfm' with "
00185 << filename << std::endl;
00186 exit(EXIT_FAILURE);
00187 }
00188
00189 image2d<float> output(nrows, ncols);
00190 for (int row = 0; row < nrows; ++row)
00191 file.read((char*)(&(output(row, 0))),
00192 sizeof(float) * ncols);
00193 file.close();
00194 return output;
00195 }
00196
00197
00198 }
00199
00200 }
00201
00202
00203 #endif // ! OLENA_LRDE_EFIGI_IO_HH