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

save.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_SAVE_HH
00027 # define MLN_IO_MAGICK_SAVE_HH
00028 
00035 
00036 # include <mln/core/image/image2d.hh>
00037 # include <mln/metal/equal.hh>
00038 # include <mln/value/int_u8.hh>
00039 # include <mln/value/rgb8.hh>
00040 # include <Magick++.h>
00041 
00042 
00043 namespace mln
00044 {
00045 
00046   namespace io
00047   {
00048 
00049     namespace magick
00050     {
00051 
00057       template <typename I>
00058       void save(const Image<I>& ima,
00059                 const std::string& filename);
00060 
00061 
00062 # ifndef MLN_INCLUDE_ONLY
00063 
00064       inline
00065       Magick::Color get_color(bool value)
00066       {
00067         std::cout << "bool color!" << std::endl;
00068         return Magick::ColorMono(value);
00069       }
00070 
00071       inline
00072       Magick::Color get_color(const value::int_u8& value)
00073       {
00074         return Magick::ColorGray(256 - value);
00075       }
00076 
00077       inline
00078       Magick::Color get_color(const value::rgb8& value)
00079       {
00080         return Magick::ColorRGB(256 - value.red(),
00081                                 256 - value.green(),
00082                                 256 - value.blue());
00083       }
00084 
00085       template <typename I>
00086       inline
00087       void save(const Image<I>& ima_, const std::string& filename)
00088       {
00089         trace::entering("mln::io::magick::save");
00090 
00091         mln_precondition(mln_site_(I)::dim == 2);
00092         const I& ima = exact(ima_);
00093         if (!(mln::metal::equal<mln_value(I), bool>::value ||
00094             mln::metal::equal<mln_value(I), value::int_u8>::value ||
00095             mln::metal::equal<mln_value(I), value::rgb8>::value))
00096         {
00097           std::cerr << "error: trying to save an unsupported format" << std::endl;
00098           std::cerr << "supported formats: binary, 8bits grayscale (int_u8), 8bits truecolor (rgb8)" << std::endl;
00099           abort();
00100         }
00101 
00102         Magick::Image im_file;
00103         im_file.size(Magick::Geometry(ima.nrows(), ima.ncols()));
00104 
00105         Magick::PixelPacket* pixel_cache = im_file.getPixels(0, 0, ima.nrows(), ima.ncols());
00106         Magick::PixelPacket* pixel;
00107         mln_piter(I) p(ima.domain());
00108         for_all(p)
00109         {
00110           pixel = pixel_cache + (int) p.to_site().to_vec()[0] * ima.ncols()
00111                               + (int) p.to_site().to_vec()[1];
00112           *pixel = get_color(ima(p));
00113         }
00114         im_file.syncPixels();
00115         im_file.write(filename);
00116 
00117         trace::exiting("mln::io::magick::save");
00118       }
00119 
00120 
00121 # endif // ! MLN_INCLUDE_ONLY
00122 
00123     } // end of namespace mln::io::magick
00124 
00125   } // end of namespace mln::io
00126 
00127 } // end of namespace mln
00128 
00129 
00130 #endif // ! MLN_IO_MAGICK_SAVE_HH

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