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

load.hh

00001 // Copyright (C) 2009, 2010 EPITA Research and Development Laboratory
00002 // (LRDE)
00003 //
00004 // This file is part of Olena.
00005 //
00006 // Olena is free software: you can redistribute it and/or modify it under
00007 // the terms of the GNU General Public License as published by the Free
00008 // Software Foundation, version 2 of the License.
00009 //
00010 // Olena is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software project 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 produce
00022 // an executable, this file does not by itself cause the resulting
00023 // executable to be covered by the GNU General Public License.  This
00024 // exception does not however invalidate any other reasons why the
00025 // executable file might be covered by the GNU General Public License.
00026 
00027 #ifndef MLN_IO_MAGICK_LOAD_HH
00028 # define MLN_IO_MAGICK_LOAD_HH
00029 
00038 
00039 # include <cstdlib>
00040 
00041 # include <Magick++.h>
00042 
00043 # include <mln/core/image/image2d.hh>
00044 
00045 # include <mln/value/int_u8.hh>
00046 # include <mln/value/rgb8.hh>
00047 
00048 
00049 namespace mln
00050 {
00051 
00052   namespace io
00053   {
00054 
00055     namespace magick
00056     {
00057 
00062       template <typename I>
00063       void load(Image<I>& ima, const std::string& filename);
00064 
00065 
00066       // FIXME: Unfinished?
00067 #if 0
00068 
00073       template <typename T>
00074       void load(Image<tiled2d<T> >& ima, const std::string& filename);
00075 #endif
00076 
00077 
00078 # ifndef MLN_INCLUDE_ONLY
00079 
00080       namespace impl
00081       {
00082 
00083         inline
00084         bool
00085         do_it(const value::rgb8& in, bool& out)
00086         {
00087           if (in.red() != in.green() || in.green() != in.blue())
00088             {
00089               std::cerr <<
00090                 "error: attempt to load what looks like a color\n"
00091                 "(mln::value::rgb8) image into a Boolean (bool) image" <<
00092                 std::endl;
00093               return false;
00094             }
00095           if (in.red() != 0 &&
00096               in.red() != mln_max(value::rgb8::red_t))
00097             {
00098               std::cerr <<
00099                 "error: attempt to load what looks like a grayscale\n"
00100                 "(mln::value::int_u8) image into a Boolean (bool) image" <<
00101                 std::endl;
00102               return false;
00103             }
00104 
00105           out = (in.red() != 0);
00106           return true;
00107         }
00108 
00109         inline
00110         bool
00111         do_it(const value::rgb8& in, value::int_u8& out)
00112         {
00113           if (in.red() != in.green() || in.green() != in.blue())
00114             {
00115               std::cerr <<
00116                 "error: attempt to load what looks like a color\n"
00117                 "(mln::value::rgb8) image into a grayscale\n"
00118                 "(mln::int_u8 values) image" << std::endl;
00119               return false;
00120             }
00121 
00122           out = in.red();
00123           return true;
00124         }
00125 
00126         inline
00127         bool
00128         do_it(const value::rgb8& in, value::rgb8& out)
00129         {
00130           out = in;
00131           return true;
00132         }
00133 
00134       } // end of namespace mln::io::magick::impl
00135 
00136 
00137       template <typename I>
00138       inline
00139       void load(Image<I>& ima_, const std::string& filename)
00140       {
00141         trace::entering("mln::io::magick::load");
00142 
00143         I& ima = exact(ima_);
00144 
00145         // FIXME: Handle Magick++'s exceptions (see either
00146         // ImageMagick++'s or GraphicsMagick++'s documentation).
00147         Magick::Image magick_ima(filename);
00148         magick_ima.read(filename);
00149         magick_ima.type(Magick::TrueColorType);
00150         int nrows = magick_ima.rows();
00151         int ncols = magick_ima.columns();
00152         mln_site(I) pmin(0, 0);
00153         mln_site(I) pmax(nrows - 1, ncols - 1);
00154 
00155         mln_concrete(I) result(box<mln_site(I)>(pmin, pmax));
00156         initialize(ima, result);
00157 
00158         Magick::Pixels view(magick_ima);
00159         // Note that `ncols' is passed before `nrows'.
00160         Magick::PixelPacket* pixels = view.get(0, 0, ima.ncols(), ima.nrows());
00161         mln_piter(I) p(ima.domain());
00162         for_all(p)
00163         {
00164           /* Each channel of a Magick++ image is coded on a
00165              Magick::Quantum value, which can be an 8-, 16- or 32-bit
00166              integer.  Load the most significant bits of each channel
00167              into a component of an mln::value::rgb8 value (i.e., into
00168              an mln::value::int_u8 value).  */
00169           value::rgb8 c(pixels->red   >> 8 * (sizeof(Magick::Quantum)
00170                                               - sizeof(value::rgb8::red_t)),
00171                         pixels->green >> 8 * (sizeof(Magick::Quantum)
00172                                               - sizeof(value::rgb8::green_t)),
00173                         pixels->blue  >> 8 * (sizeof(Magick::Quantum)
00174                                               - sizeof(value::rgb8::blue_t)));
00175           mln_value(I) res;
00176           if (!impl::do_it(c, res))
00177             {
00178               std::cerr << "while trying to load `" << filename << "'"
00179                         << std::endl;
00180               abort();
00181             }
00182           ima(p) = res;
00183           ++pixels;
00184         }
00185 
00186         trace::exiting("mln::io::magick::load");
00187       }
00188 
00189 
00190       // FIXME: Unfinished?
00191 #if 0
00192       template<typename T>
00193       inline
00194       void
00195       load(Image<tiled2d<T> >& ima_, const std::string& filename)
00196       {
00197         trace::entering("mln::io::magick::load");
00198 
00199         tiled2d<T>& ima = exact(ima_);
00200 
00201         tiled2d<T> result(filename);
00202 
00203         ima = result;
00204         trace::exiting("mln::io::magick::load");
00205       }
00206 #endif
00207 
00208 
00209 # endif // ! MLN_INCLUDE_ONLY
00210 
00211     } // end of namespace mln::io::magick
00212 
00213   } // end of namespace mln::io
00214 
00215 } // end of namespace mln
00216 
00217 
00218 #endif // ! MLN_IO_MAGICK_LOAD_HH

Generated on Fri Oct 19 2012 04:15:58 for Milena (Olena) by  doxygen 1.7.1