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