Milena (Olena)
User documentation 2.0a Id
|
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 def::coord 00159 minrow = geom::min_row(ima), 00160 mincol = geom::min_col(ima), 00161 maxrow = geom::max_row(ima), 00162 maxcol = geom::max_col(ima); 00163 00164 Magick::Pixels view(magick_ima); 00165 // Note that `ncols' is passed before `nrows'. 00166 Magick::PixelPacket* pixels = view.get(0, 0, ima.ncols(), ima.nrows()); 00167 mln_value(I) *ptr_ima = &ima(ima.domain().pmin()); 00168 00169 unsigned row_offset = ima.delta_index(dpoint2d(+1, - ncols)); 00170 00171 for (def::coord row = minrow; row <= maxrow; 00172 ++row, ptr_ima += row_offset) 00173 for (def::coord col = mincol; col <= maxcol; ++col) 00174 { 00175 00176 /* Each channel of a Magick++ image is coded on a 00177 Magick::Quantum value, which can be an 8-, 16- or 32-bit 00178 integer. Load the most significant bits of each channel 00179 into a component of an mln::value::rgb8 value (i.e., into 00180 an mln::value::int_u8 value). */ 00181 value::rgb8 c(pixels->red >> 8 * (sizeof(Magick::Quantum) 00182 - sizeof(value::rgb8::red_t)), 00183 pixels->green >> 8 * (sizeof(Magick::Quantum) 00184 - sizeof(value::rgb8::green_t)), 00185 pixels->blue >> 8 * (sizeof(Magick::Quantum) 00186 - sizeof(value::rgb8::blue_t))); 00187 mln_value(I) res; 00188 if (!impl::do_it(c, res)) 00189 { 00190 std::cerr << "while trying to load `" << filename << "'" 00191 << std::endl; 00192 abort(); 00193 } 00194 *ptr_ima++ = res; 00195 ++pixels; 00196 } 00197 00198 trace::exiting("mln::io::magick::load"); 00199 } 00200 00201 00202 // FIXME: Unfinished? 00203 #if 0 00204 template<typename T> 00205 inline 00206 void 00207 load(Image<tiled2d<T> >& ima_, const std::string& filename) 00208 { 00209 trace::entering("mln::io::magick::load"); 00210 00211 tiled2d<T>& ima = exact(ima_); 00212 00213 tiled2d<T> result(filename); 00214 00215 ima = result; 00216 trace::exiting("mln::io::magick::load"); 00217 } 00218 #endif 00219 00220 00221 # endif // ! MLN_INCLUDE_ONLY 00222 00223 } // end of namespace mln::io::magick 00224 00225 } // end of namespace mln::io 00226 00227 } // end of namespace mln 00228 00229 00230 #endif // ! MLN_IO_MAGICK_LOAD_HH