Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
fits/load.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_IO_FITS_LOAD_HH
27 # define MLN_IO_FITS_LOAD_HH
28 
33 
34 # include <iostream>
35 # include <fstream>
36 # include <string>
37 
38 # include <mln/core/image/image2d.hh>
39 # include <mln/value/int_u8.hh>
40 
41 # include <fitsio.h>
42 
43 
44 namespace mln
45 {
46 
47  namespace io
48  {
49 
50 
51  namespace fits
52  {
53 
60  void load(image2d<float>& ima,
61  const std::string& filename);
62 
69  image2d<float> load(const std::string& filename);
70 
71 # ifndef MLN_INCLUDE_ONLY
72 
73  inline
74  void fits_exit(int status)
75  {
76  if (status)
77  {
78  fits_report_error(stderr, status);
79  exit(status);
80  }
81  return;
82  }
83 
84  inline
85  image2d<float> load(const std::string& filename)
86  {
87  trace::entering("mln::io::fits::load");
88 
89  fitsfile *fptr;
90  int status, nfound, anynull;
91  long naxes[2];
92  float nullval;
93 
94  status = 0;
95  if (fits_open_file(&fptr, filename.c_str(), READONLY, &status))
96  fits_exit(status);
97 
98  char NAXIS[] = "NAXIS";
99  if (fits_read_keys_lng(fptr, NAXIS, 1, 2, naxes, &nfound, &status))
100  fits_exit(status);
101 
102  const int ncols = naxes[0], nrows = naxes[1];
103 
104  image2d<float> output(nrows, ncols);
105 
106  nullval = 0; // don't check null values
107 
108  point2d p(point2d(0, 0));
109 
110  for (p.row() = 0; p.row() < nrows; ++p.row())
111  {
112  if (fits_read_img(fptr,
113  TFLOAT,
114  1 + p.row() * ncols,
115  ncols,
116  &nullval,
117  &(output(p)),
118  &anynull,
119  &status))
120  fits_exit(status);
121  }
122 
123  if (fits_close_file(fptr, &status))
124  fits_exit(status);
125 
126  trace::exiting("mln::io::fits::load");
127 
128  return output;
129  }
130 
131  inline
132  void load(image2d<float>& ima,
133  const std::string& filename)
134  {
135  ima = load(filename);
136  }
137 
138 # endif // ! MLN_INCLUDE_ONLY
139 
140  } // end of namespace mln::io::fits
141 
142  } // end of namespace mln::io
143 
144 } // end of namespace mln
145 
146 
147 #endif // ! MLN_IO_FITS_LOAD_HH