• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

load.hh

00001 // Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_IO_MAGICK_LOAD_HH
00027 # define MLN_IO_MAGICK_LOAD_HH
00028 
00033 
00034 # include <mln/core/image/image2d.hh>
00035 # include <mln/value/int_u8.hh>
00036 # include <mln/value/rgb8.hh>
00037 # include <Magick++.h>
00038 
00039 
00040 namespace mln
00041 {
00042 
00043   namespace io
00044   {
00045 
00046     namespace magick
00047     {
00048 
00055       template <typename I>
00056       void load(Image<I>& ima,
00057                 const std::string& filename);
00058 
00059 
00060 # ifndef MLN_INCLUDE_ONLY
00061 
00062       inline
00063       bool do_it(const value::rgb8& in, bool& out, const std::string& filename)
00064       {
00065         if (in.red() == 255u && in.green() == 255u && in.blue() == 255u)
00066         {
00067           out = true;
00068           return true;
00069         }
00070         if (in.red() == 0u && in.green() == 0u && in.blue() == 0u)
00071         {
00072           out = false;
00073           return true;
00074         }
00075         if (in.red() == in.green() && in.green() == in.blue())
00076           std::cerr << "error: trying to load '" << filename << "' which is a grayscale image into a bool image" << std::endl;
00077         else
00078           std::cerr << "error: trying to load '" << filename << "' which is a truecolor image into a bool image" << std::endl;
00079         return false;
00080       }
00081 
00082       inline
00083       bool do_it(const value::rgb8& in, value::int_u8& out, const std::string& filename)
00084       {
00085         if (in.red() == in.green() && in.green() == in.blue())
00086         {
00087           out = in.red();
00088           return true;
00089         }
00090         std::cerr << "error: trying to load '" << filename << "' which is a truecolor image into a grayscale image" << std::endl;
00091         return false;
00092       }
00093 
00094       inline
00095       bool do_it(const value::rgb8& in, value::rgb8& out, const std::string& filename)
00096       {
00097         (void) filename;
00098         out = in;
00099         return true;
00100       }
00101 
00102 
00103       template <typename I>
00104       inline
00105       void load(Image<I>& ima_,
00106           const std::string& filename)
00107       {
00108         trace::entering("mln::io::magick::load");
00109 
00110         I& ima = exact(ima_);
00111 
00112         //std::ifstream file(filename.c_str());
00113         //if (! file)
00114         //{
00115         //  std::cerr << "error: cannot open file '" << filename << "'!";
00116         //  abort();
00117         //}
00118 
00119         Magick::Image im_file(filename);
00120         im_file.modifyImage();
00121         im_file.type(Magick::TrueColorType);
00122         int columns = im_file.columns();
00123         int rows = im_file.rows();
00124         /*std::cout << "width: " <<columns << std::endl;
00125           std::cout << "height: " <<rows << std::endl;
00126           std::cout << "depth: " <<im_file.depth() << std::endl;
00127           std::cout << "format: " <<im_file.format() << std::endl;
00128           std::cout << "magick: " <<im_file.magick() << std::endl;*/
00129 
00130         const Magick::PixelPacket *pixel_cache = im_file.getConstPixels(0, 0, columns, rows);
00131 
00132         algebra::vec<mln_site_(I)::dim, unsigned int> vmin;
00133         algebra::vec<mln_site_(I)::dim, unsigned int> vmax;
00134         vmin[0] = 0;
00135         vmin[1] = 0;
00136         vmax[0] = rows - 1;
00137         vmax[1] = columns - 1;
00138         mln_site(I) pmin(vmin);
00139         mln_site(I) pmax(vmax);
00140         mln_concrete(I) result(box<mln_site(I)>(pmin, pmax));
00141         initialize(ima, result);
00142         mln_piter(I) p(ima.domain());
00143         for_all(p)
00144         {
00145           const Magick::PixelPacket *pixel = pixel_cache + (int) p.to_site().to_vec()[0] * columns
00146                                              + (int) p.to_site().to_vec()[1];
00147           // FIXME: Quantum = 16bits but rgb is 8bits
00148           value::rgb8 pix(pixel->red % 256, pixel->green % 256, pixel->blue % 256);
00149           mln_value(I) res;
00150           if (!do_it(pix, res, filename))
00151             abort();
00152           ima(p) = res;
00153         }
00154 
00155         trace::exiting("mln::io::magick::load");
00156       }
00157 
00158 
00159 # endif // ! MLN_INCLUDE_ONLY
00160 
00161     } // end of namespace mln::io::magick
00162 
00163   } // end of namespace mln::io
00164 
00165 } // end of namespace mln
00166 
00167 
00168 #endif // ! MLN_IO_MAGICK_LOAD_HH

Generated on Thu Sep 8 2011 18:32:04 for Milena (Olena) by  doxygen 1.7.1