26 #ifndef MLN_IO_TIFF_LOAD_HH
27 # define MLN_IO_TIFF_LOAD_HH
43 # include <mln/core/concept/image.hh>
44 # include <mln/value/rgb8.hh>
61 void load(Image<I>& ima_,
const std::string& filename);
65 # ifndef MLN_INCLUDE_ONLY
72 point2d ij2rc_1(
int i,
int j,
int ni_1,
int nj_1)
79 point2d ij2rc_2(
int i,
int j,
int ni_1,
int nj_1)
81 return point2d(ni_1 - i, nj_1 - j);
85 point2d ij2rc_3(
int i,
int j,
int ni_1,
int nj_1)
92 point2d ij2rc_4(
int i,
int j,
int ni_1,
int nj_1)
100 point2d ij2rc_5(
int i,
int j,
int ni_1,
int nj_1)
107 point2d ij2rc_6(
int i,
int j,
int ni_1,
int nj_1)
109 return point2d(nj_1 - j, ni_1 - i);
113 point2d ij2rc_7(
int i,
int j,
int ni_1,
int nj_1)
120 point2d ij2rc_8(
int i,
int j,
int ni_1,
int nj_1)
129 template <
typename I>
131 I load_header(TIFF *file)
133 uint32 width, height;
135 TIFFGetField(file, TIFFTAG_IMAGEWIDTH, &width);
136 TIFFGetField(file, TIFFTAG_IMAGELENGTH, &height);
138 mln_concrete(I) new_ima(height, width, 0);
144 template <typename I>
146 void load_data_rgb8(I& ima, TIFF *file)
148 uint16 bits_per_sample, samples_per_pixel;
150 TIFFGetField(file, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
151 TIFFGetField(file, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);
153 uint16 data_size = bits_per_sample * samples_per_pixel;
154 if (data_size != 24 && data_size != 32)
156 std::cout <<
"Trying to load a non color TIFF "
157 <<
"image into a color Milena image." << std::endl;
161 uint32 npixels = ima.ncols() * ima.nrows();
162 uint32 *raster = (uint32 *) _TIFFmalloc(npixels *
sizeof (uint32));
164 if (!TIFFReadRGBAImage(file, ima.ncols(), ima.nrows(), raster, 0))
166 std::cout <<
"Error while reading the image file. Is it corrupted?"
172 TIFFGetField(file, TIFFTAG_ORIENTATION, &orientation);
174 typedef point2d (*fun_t)(int, int, int, int);
175 fun_t funs[] = { 0, ij2rc_1, ij2rc_2, ij2rc_3, ij2rc_4,
176 ij2rc_5, ij2rc_6, ij2rc_7, ij2rc_8 };
177 fun_t fun = funs[orientation];
182 if (orientation <= 4)
184 ni_1 = ima.nrows() - 1;
185 nj_1 = ima.ncols() - 1;
186 for (
int i = 0; i <= ni_1; ++i)
187 for (
int j = 0; j <= nj_1; ++j)
191 v.red() = (
unsigned char) TIFFGetR(raster[idx]);
192 v.green() = (
unsigned char) TIFFGetG(raster[idx]);
193 v.blue() = (
unsigned char) TIFFGetB(raster[idx]);
194 ima((*fun)(i, j, ni_1, nj_1)) = v;
201 nj_1 = ima.nrows() - 1;
202 ni_1 = ima.ncols() - 1;
203 for (
int j = 0; j <= nj_1; ++j)
204 for (
int i = 0; i <= ni_1; ++i)
208 v.red() = (
unsigned char) TIFFGetR(raster[idx]);
209 v.green() = (
unsigned char) TIFFGetG(raster[idx]);
210 v.blue() = (
unsigned char) TIFFGetB(raster[idx]);
211 ima((*fun)(i, j, ni_1, nj_1)) = v;
223 template <
typename I>
225 void load_data_scalar(I& ima, TIFF *file)
227 uint16 samples_per_pixel;
228 TIFFGetField(file, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);
229 if (samples_per_pixel != 1)
231 std::cout <<
"Trying to load a non grayscale TIFF "
232 <<
"image into a grayscale Milena image." << std::endl;
236 uint32 npixels = ima.ncols() * ima.nrows();
237 uint32 *raster = (uint32 *) _TIFFmalloc(npixels *
sizeof (uint32));
239 if (!TIFFReadRGBAImage(file, ima.ncols(), ima.nrows(), raster, 0))
241 std::cout <<
"Error while reading the image file. Is it corrupted?"
247 TIFFGetField(file, TIFFTAG_ORIENTATION, &orientation);
249 typedef point2d (*fun_t)(int, int, int, int);
250 fun_t funs[] = { 0, ij2rc_1, ij2rc_2, ij2rc_3, ij2rc_4,
251 ij2rc_5, ij2rc_6, ij2rc_7, ij2rc_8 };
252 fun_t fun = funs[orientation];
257 if (orientation <= 4)
259 ni_1 = ima.nrows() - 1;
260 nj_1 = ima.ncols() - 1;
261 for (
int i = 0; i <= ni_1; ++i)
262 for (
int j = 0; j <= nj_1; ++j)
264 ima((*fun)(i, j, ni_1, nj_1)) = (
unsigned char) TIFFGetR(raster[idx++]);
269 nj_1 = ima.nrows() - 1;
270 ni_1 = ima.ncols() - 1;
271 for (
int j = 0; j <= nj_1; ++j)
272 for (
int i = 0; i <= ni_1; ++i)
274 ima((*fun)(i, j, ni_1, nj_1)) = (
unsigned char) TIFFGetR(raster[idx++]);
281 template <
typename I>
284 load_data_dispatch(
const value::rgb8&, I& ima, TIFF *file)
286 load_data_rgb8(ima, file);
289 template <
typename S,
typename I>
292 load_data_dispatch(
const value::Scalar<S>&, I& ima, TIFF *file)
294 load_data_scalar(ima, file);
297 template <
typename I>
300 load_data_dispatch(
const bool&, I& ima, TIFF *file)
302 load_data_scalar(ima, file);
306 template <
typename I>
309 load_data_dispatch(I& ima, TIFF *file)
311 load_data_dispatch(mln_value(I)(), exact(ima), file);
321 template <
typename I>
325 trace::entering(
"mln::io::tiff::load");
327 I& ima = exact(ima_);
329 TIFF *file = TIFFOpen(filename.c_str(),
"r");
332 std::cerr <<
"io::tiff::load - Error: cannot open file '"
338 ima = internal::load_header<I>(file);
339 internal::load_data_dispatch(ima, file);
341 mln_postcondition(ima.is_valid());
343 (void) TIFFClose(file);
344 trace::exiting(
"mln::io::tiff::load");
347 # endif // ! MLN_INCLUDE_ONLY
355 #endif // ! MLN_IO_TIFF_LOAD_HH