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

save.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_SAVE_HH
00028 # define MLN_IO_MAGICK_SAVE_HH
00029 
00038 
00039 # include <cstdlib>
00040 
00041 # include <Magick++.h>
00042 
00043 # include <mln/metal/equal.hh>
00044 
00045 # include <mln/core/image/image2d.hh>
00046 
00047 # include <mln/value/int_u8.hh>
00048 # include <mln/value/rgb8.hh>
00049 # include <mln/value/qt/rgb32.hh>
00050 
00051 
00052 namespace mln
00053 {
00054 
00055   namespace io
00056   {
00057 
00058     namespace magick
00059     {
00060 
00065       template <typename I>
00066       void
00067       save(const Image<I>& ima, const std::string& filename);
00068 
00069 
00070       // FIXME: Unfinished?
00071 #if 0
00072 
00076       template <typename T>
00077       void
00078       save(const Image< tiled2d<T> >& ima, const std::string& filename);
00079 #endif
00080 
00081 
00082 # ifndef MLN_INCLUDE_ONLY
00083 
00084       namespace impl
00085       {
00086 
00087         inline
00088         Magick::Color get_color(bool value)
00089         {
00090           return Magick::ColorMono(value);
00091         }
00092 
00093         inline
00094         Magick::Color get_color(const value::int_u8& value)
00095         {
00096           /* Each channel of a Magick++ image is coded on a
00097              Magick::Quantum value, which can be an 8-, 16- or 32-bit
00098              integer.  Store the data from each mln::value::int_u8
00099              values into the most significant bits of Magick::Color's
00100              channels.  */
00101           return Magick::Color
00102             (value << 8 * (sizeof(Magick::Quantum) - sizeof(value::int_u8)),
00103              value << 8 * (sizeof(Magick::Quantum) - sizeof(value::int_u8)),
00104              value << 8 * (sizeof(Magick::Quantum) - sizeof(value::int_u8)));
00105         }
00106 
00107         inline
00108         Magick::Color get_color(const value::rgb8& value)
00109         {
00110           /* Each channel of a Magick++ image is coded on a
00111              Magick::Quantum value, which can be an 8-, 16- or 32-bit
00112              integer.  Store the data from each component of
00113              mln::value::rgb8 values (of type mln::value::int_u8) into
00114              the most significant bits of Magick::Color's
00115              channels.  */
00116           return Magick::Color
00117             (value.red()   << 8 * (sizeof(Magick::Quantum)
00118                                    - sizeof(value::rgb8::red_t)),
00119              value.green() << 8 * (sizeof(Magick::Quantum)
00120                                    - sizeof(value::rgb8::green_t)),
00121              value.blue()  << 8 * (sizeof(Magick::Quantum)
00122                                    - sizeof(value::rgb8::blue_t)));
00123         }
00124 
00125       } // end of namespace mln::io::magick::impl
00126 
00127 
00128       template <typename I>
00129       inline
00130       void
00131       save(const Image<I>& ima_, const std::string& filename)
00132       {
00133         trace::entering("mln::io::magick::save");
00134 
00135         mln_precondition(mln_site_(I)::dim == 2);
00136         // Turn this into a static check?
00137         if (!(mln::metal::equal<mln_value(I), bool>::value ||
00138             mln::metal::equal<mln_value(I), value::int_u8>::value ||
00139             mln::metal::equal<mln_value(I), value::rgb8>::value ||
00140               mln::metal::equal<mln_value(I), value::qt::rgb32>::value))
00141         {
00142           std::cerr <<
00143             "error: trying to save an unsupported format\n"
00144             "supported formats are:\n"
00145             "  binary (bool)\n"
00146             "  8-bit grayscale (mln::value::int_u8)\n"
00147             "  3x8-bit truecolor (rgb8)" << std::endl;
00148           abort();
00149         }
00150 
00151         const I& ima = exact(ima_);
00152 
00153         Magick::Image magick_ima;
00154         // In the construction of a Geometry object, the width (i.e.
00155         // `ncols') comes first, then the height (i.e. `nrows')
00156         // follows.
00157         magick_ima.size(Magick::Geometry(ima.ncols(), ima.nrows()));
00158 
00159         Magick::Pixels view(magick_ima);
00160         // As above, `ncols' is passed before `nrows'.
00161         Magick::PixelPacket* pixels = view.get(0, 0, ima.ncols(), ima.nrows());
00162         mln_piter(I) p(ima.domain());
00163         for_all(p)
00164           *pixels++ = impl::get_color(ima(p));
00165 
00166         view.sync();
00167         magick_ima.write(filename);
00168 
00169         trace::exiting("mln::io::magick::save");
00170       }
00171 
00172 
00173       // FIXME: Unfinished?
00174 #if 0
00175       template <typename T>
00176       void
00177       save(const Image< tiled2d<T> >& ima_, const std::string& filename)
00178       {
00179         trace::entering("mln::io::magick::save");
00180 
00181         tiled2d<T>& ima = exact(ima_);
00182 
00183         ima.buffer().write(filename);
00184 
00185         trace::exiting("mln::io::magick::save");
00186       }
00187 #endif
00188 
00189 
00190 # endif // ! MLN_INCLUDE_ONLY
00191 
00192     } // end of namespace mln::io::magick
00193 
00194   } // end of namespace mln::io
00195 
00196 } // end of namespace mln
00197 
00198 
00199 #endif // ! MLN_IO_MAGICK_SAVE_HH

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