-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image_reader_2.h
115 lines (93 loc) · 3.02 KB
/
Image_reader_2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright (C) 2012
* Helmholtz Centre for Environmental Research (UFZ)
*
* This file is part of "GeoMeshCo." "GeoMeshCo" is free
* software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* "GeoMeshCo" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* "GeoMeshCo". If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, you have permission to link this program
* with the Computational Geometry Algorithms Library (CGAL,
* <http://www.cgal.org>) and distribute executables, as long as you
* follow the requirements of the GNU GPL in regard to all of
* the software in the executable aside from CGAL.
*
*
* Author: Dmitrij Yu. Naumov
*
*/
#ifndef IMAGE_READER_2
#define IMAGE_READER_2
#include <algorithm>
#include <string>
#include <vector>
#include <tiffio.h>
template <typename T, typename I> class Image_2;
class Image_reader_2
{
public:
Image_reader_2(const std::string& filename)
: _filename(filename)
{ }
virtual ~Image_reader_2()
{ }
protected:
const std::string _filename;
};
class TIFF_image_reader_2
: public Image_reader_2
{
public:
TIFF_image_reader_2(const std::string& filename)
: Image_reader_2(filename),
tiff_file(0), _bpp(0), _spp(0), _width(0), _height(0)
{
tiff_file = TIFFOpen(_filename.c_str(), "r");
if (tiff_file == nullptr)
throw;
TIFFGetField(tiff_file, TIFFTAG_BITSPERSAMPLE, &_bpp);
TIFFGetField(tiff_file, TIFFTAG_SAMPLESPERPIXEL, &_spp);
TIFFGetField(tiff_file, TIFFTAG_IMAGELENGTH, &_height);
TIFFGetField(tiff_file, TIFFTAG_IMAGEWIDTH, &_width);
}
virtual ~TIFF_image_reader_2()
{
_TIFFfree(tiff_file);
}
size_t
bpp() const
{
return _bpp;
}
template <typename PixelT, typename InterpolationT>
Image_2<PixelT, InterpolationT>*
get_image() const
{
const uint32 linesize = TIFFScanlineSize(tiff_file);
std::vector<PixelT> data(_width * _height);
// Linesize must be equal to width, otherwise an image will be
// incomplete.
if (linesize/(_bpp/8*_spp) != _width)
throw;
for (uint32 i = 0; i < _height; i++)
TIFFReadScanline(tiff_file, &data[_width*i], i, 0);
return new Image_2<PixelT, InterpolationT>(_width, _height, &data[0]);
}
private:
TIFF* tiff_file;
uint32 _bpp, _spp;
uint32 _width, _height;
private:
TIFF_image_reader_2(const TIFF_image_reader_2&);
TIFF_image_reader_2 operator=(const TIFF_image_reader_2&);
};
#endif // IMAGE_READER_2