27 #ifndef MLN_IO_TIFF_LOAD_HH
28 # define MLN_IO_TIFF_LOAD_HH
44 # include <mln/core/concept/image.hh>
45 # include <mln/value/rgb8.hh>
68 # ifndef MLN_INCLUDE_ONLY
75 point2d ij2rc_1(
int i,
int j,
int ni_1,
int nj_1)
82 point2d ij2rc_2(
int i,
int j,
int ni_1,
int nj_1)
84 return point2d(ni_1 - i, nj_1 - j);
88 point2d ij2rc_3(
int i,
int j,
int ni_1,
int nj_1)
95 point2d ij2rc_4(
int i,
int j,
int ni_1,
int nj_1)
103 point2d ij2rc_5(
int i,
int j,
int ni_1,
int nj_1)
110 point2d ij2rc_6(
int i,
int j,
int ni_1,
int nj_1)
112 return point2d(nj_1 - j, ni_1 - i);
116 point2d ij2rc_7(
int i,
int j,
int ni_1,
int nj_1)
123 point2d ij2rc_8(
int i,
int j,
int ni_1,
int nj_1)
132 template <
typename I>
134 I load_header(TIFF *file)
138 TIFFGetField(file, TIFFTAG_IMAGEWIDTH, &width);
139 TIFFGetField(file, TIFFTAG_IMAGELENGTH, &height);
141 mln_concrete(I) new_ima(height, width, 0);
147 template <typename I>
149 void load_data_rgb8(I& ima, TIFF *file)
151 uint16 bits_per_sample, samples_per_pixel;
153 TIFFGetField(file, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
154 TIFFGetField(file, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);
156 uint16 data_size = bits_per_sample * samples_per_pixel;
157 if (data_size != 24 && data_size != 32)
159 std::cerr <<
"Trying to load a non color TIFF "
160 <<
"image into a color Milena image." << std::endl;
164 uint32 npixels = ima.ncols() * ima.nrows();
165 uint32 *raster = (uint32 *) _TIFFmalloc(npixels *
sizeof (uint32));
167 if (!TIFFReadRGBAImage(file, ima.ncols(), ima.nrows(), raster, 0))
169 std::cerr <<
"Error while reading the image file. Is it corrupted?"
175 TIFFGetField(file, TIFFTAG_ORIENTATION, &orientation);
178 fun_t funs[] = { 0, ij2rc_1, ij2rc_2, ij2rc_3, ij2rc_4,
179 ij2rc_5, ij2rc_6, ij2rc_7, ij2rc_8 };
180 fun_t fun = funs[orientation];
185 if (orientation <= 4)
187 ni_1 = ima.nrows() - 1;
188 nj_1 = ima.ncols() - 1;
189 for (
int i = 0; i <= ni_1; ++i)
190 for (
int j = 0; j <= nj_1; ++j)
194 v.red() = (
unsigned char) TIFFGetR(raster[idx]);
195 v.green() = (
unsigned char) TIFFGetG(raster[idx]);
196 v.blue() = (
unsigned char) TIFFGetB(raster[idx]);
197 ima((*fun)(i, j, ni_1, nj_1)) = v;
204 nj_1 = ima.nrows() - 1;
205 ni_1 = ima.ncols() - 1;
206 for (
int j = 0; j <= nj_1; ++j)
207 for (
int i = 0; i <= ni_1; ++i)
211 v.red() = (
unsigned char) TIFFGetR(raster[idx]);
212 v.green() = (
unsigned char) TIFFGetG(raster[idx]);
213 v.blue() = (
unsigned char) TIFFGetB(raster[idx]);
214 ima((*fun)(i, j, ni_1, nj_1)) = v;
226 template <
typename I>
228 void load_data_scalar(I& ima, TIFF *file)
230 uint16 samples_per_pixel;
231 TIFFGetField(file, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);
232 if (samples_per_pixel != 1)
234 std::cerr <<
"Trying to load a non grayscale TIFF "
235 <<
"image into a grayscale Milena image." << std::endl;
239 uint32 npixels = ima.ncols() * ima.nrows();
240 uint32 *raster = (uint32 *) _TIFFmalloc(npixels *
sizeof (uint32));
242 if (!TIFFReadRGBAImage(file, ima.ncols(), ima.nrows(), raster, 0))
244 std::cerr <<
"Error while reading the image file. Is it corrupted?"
250 TIFFGetField(file, TIFFTAG_ORIENTATION, &orientation);
252 typedef point2d (*fun_t)(int, int, int, int);
253 fun_t funs[] = { 0, ij2rc_1, ij2rc_2, ij2rc_3, ij2rc_4,
254 ij2rc_5, ij2rc_6, ij2rc_7, ij2rc_8 };
255 fun_t fun = funs[orientation];
260 if (orientation <= 4)
262 ni_1 = ima.nrows() - 1;
263 nj_1 = ima.ncols() - 1;
264 for (
int i = 0; i <= ni_1; ++i)
265 for (
int j = 0; j <= nj_1; ++j)
267 ima((*fun)(i, j, ni_1, nj_1)) = (
unsigned char) TIFFGetR(raster[idx++]);
272 nj_1 = ima.nrows() - 1;
273 ni_1 = ima.ncols() - 1;
274 for (
int j = 0; j <= nj_1; ++j)
275 for (
int i = 0; i <= ni_1; ++i)
277 ima((*fun)(i, j, ni_1, nj_1)) = (
unsigned char) TIFFGetR(raster[idx++]);
284 template <
typename I>
287 load_data_dispatch(
const value::rgb8&, I& ima, TIFF *file)
289 load_data_rgb8(ima, file);
292 template <
typename S,
typename I>
295 load_data_dispatch(
const value::Scalar<S>&, I& ima, TIFF *file)
297 load_data_scalar(ima, file);
300 template <
typename I>
303 load_data_dispatch(
const bool&, I& ima, TIFF *file)
305 load_data_scalar(ima, file);
309 template <
typename I>
312 load_data_dispatch(I& ima, TIFF *file)
314 load_data_dispatch(mln_value(I)(),
exact(ima), file);
324 template <
typename I>
328 mln_trace(
"mln::io::tiff::load");
330 I& ima =
exact(ima_);
332 TIFF *file = TIFFOpen(filename.c_str(),
"r");
335 std::cerr <<
"io::tiff::load - Error: cannot open file '"
341 ima = internal::load_header<I>(file);
342 internal::load_data_dispatch(ima, file);
344 mln_postcondition(ima.is_valid());
346 (void) TIFFClose(file);
349 # endif // ! MLN_INCLUDE_ONLY
357 #endif // ! MLN_IO_TIFF_LOAD_HH