io.hh

00001 // Copyright (C) 2006  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016 // Boston, MA 02110-1301, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
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     // Fwd decls.
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     // end of fwd decls.
00058 
00059 
00060 
00061     // sample code I:
00062     /*
00063 
00064     image2d<unsigned char> lena = oln::load("lena.pgm");
00065 
00066     image2d<float> lenaf = efigi::add_float_noise(lena);
00067     efigi::save_pfm(lenaf, "lenaf.pfm");
00068 
00069     image2d<float> lenaf_2 = efigi::load_pfm("lenaf.pfm");
00070     assert(not efigi::diff(lenaf, lenaf_2));
00071 
00072     */
00073     // end of sample code
00074 
00075 
00076 
00077 
00078     // sample code II:
00079     /*
00080 
00081     image2d<float> ima = efigi::load_fits(argv[1]);
00082     float min, max;
00083     efigi::min_max(ima, min, max);
00084     std::cout << "min = " << min << "  max = " << max << std::endl;
00085 
00086     image2d<unsigned char> lin = efigi::convert_linear_8(ima, min, max);
00087     save(lin, "tmp_lin.pgm");
00088 
00089     std::vector<unsigned> h = efigi::compute_histo(lin);
00090     std::ofstream file("tmp_h8.dat");
00091     efigi::print_histo(h, file);
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; // don't check null values
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   } // end of namespace oln::efigi
00199 
00200 } // end of namespace oln
00201 
00202 
00203 #endif // ! OLENA_LRDE_EFIGI_IO_HH

Generated on Tue Feb 20 20:20:04 2007 for Olena by  doxygen 1.5.1