Loading...
Searching...
No Matches
FileTIFF.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25 */
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31#ifndef DRAIN_TIFF_H_EXPERIMENTAL
32#define DRAIN_TIFF_H_EXPERIMENTAL
33
34#include <drain/Log.h>
35#include "drain/util/FileInfo.h"
36#include "drain/util/Dictionary.h"
37#include "drain/util/Time.h"
38#include "GeoFrame.h"
39#include "Image.h"
40
41#ifndef USE_GEOTIFF_NO
42
43#include <xtiffio.h>
44#endif
45
46namespace drain
47{
48
49namespace image
50{
51
52
53
55
57class FileTIFF { //: public FileHandler{
58public:
59
60
61 static
62 const drain::FileInfo fileInfo;
63
64 // OLD (Weak?)
66
67 static const dict_t compressionDict;
68 /*
69 static
70 const dict_t & getCompressionDict();
71 */
72
73 // https://www.awaresystems.be/imaging/tiff/tifftags/compression.html
74 // https://gdal.org/drivers/raster/cog.html
75 static dict_t::value_t defaultCompression; // COMPRESSION_NONE = 1; COMPRESSION_LZW = 5;
76 // static int defaultCompressionLevel; // COMPRESSION_NONE = 1; COMPRESSION_LZW = 5;
77
78 static drain::Frame2D<int> defaultTile;
79
80
81#ifndef USE_GEOTIFF_NO
82
83
84 inline
85 FileTIFF(const std::string & path = "", const std::string & mode = "w") : tif(nullptr), tile(defaultTile){ // FileHandler(__FUNCTION__),
86 if (!path.empty())
87 open(path, mode);
88 //tif = XTIFFOpen(path.c_str(), mode);
89 }
90
91 virtual inline
92 ~FileTIFF(){
93 //mout.special("Closing FileTIFF");
94 close();
95 }
96
97 virtual inline
98 void open(const std::string & path, const std::string & mode = "w"){
99 tif = XTIFFOpen(path.c_str(), mode.c_str());
100 }
101
102 virtual inline
103 bool isOpen() const {
104 return (tif != nullptr);
105 }
106
107 virtual inline
108 void close(){
109 if (isOpen()){
110 drain::Logger mout(__FILE__, __FUNCTION__);
111 mout.debug("Closing TIFF...");
112 XTIFFClose(tif);
113 tif = nullptr;
114 }
115 }
116
117 inline
118 int setField(int tag, const std::string & value){
119 if (!isOpen()){
120 drain::Logger mout(__FILE__, __FUNCTION__);
121 mout.error("TIFF file not open");
122 }
123 return TIFFSetField(tif, tag, value.c_str());
124 }
125
126 template <class T>
127 inline
128 int setField(int tag, const std::vector<T> & value){
129 if (!isOpen()){
130 drain::Logger mout(__FILE__, __FUNCTION__);
131 mout.error("TIFF file not open");
132 //return 0;
133 }
134 return TIFFSetField(tif, tag, value.size(), &value.at(0));
135 }
136
137 template <class T>
138 inline
139 int setField(int tag, T value){
140 if (!isOpen()){
141 drain::Logger mout(__FILE__, __FUNCTION__);
142 mout.error("TIFF file not open");
143 //return 0;
144 }
145 return TIFFSetField(tif, tag, value);
146 }
147
148
149 inline
150 void useDefaultTileSize(){
151 this->tile = defaultTile;
152 }
153
154 inline
155 void setTileSize(int tileWidth, int tileHeight = 0){
156 if (tileWidth == 0){
157 tile = {0,0};
158 //this->tile = defaultTile;
159 }
160 else {
161 tile.setWidth(tileWidth);
162 if (tileHeight == 0){
163 tileHeight = tileWidth;
164 }
165 tile.setHeight(tileHeight);
166 }
167 }
168
171 void setTime(const drain::Time & time);
172
176 void setDefaults(); //, int tileWidth=0, int tileHeight = 0);
177 //void setDefaults(const drain::image::ImageConf & src); //, int tileWidth=0, int tileHeight = 0);
178
179 void writeImageData(const drain::image::Image & src);
180
182
185 static
186 void write(const std::string & path, const drain::image::Image & src);
187
188
189protected:
190
191 TIFF *tif;
192
194
195
196#endif
197
198};
199
200} // image::
201
202} // drain::
203
204
205
206#endif
Two-way mapping between strings and objects of template class T.
Definition Dictionary.h:63
Definition FileInfo.h:48
Something that has width and height.
Definition Frame.h:55
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:667
Logger & error(const TT &... args)
Echoes.
Definition Log.h:417
Utility for handling time. Internally, uses tm (C time structure).
Definition Time.h:54
For writing images in basic TIFF format. Reading not supported currently.
Definition FileTIFF.h:57
static void write(const std::string &path, const drain::image::Image &src)
Default implementation.
Definition FileTIFF.cpp:320
void writeImageData(const drain::image::Image &src)
Definition FileTIFF.cpp:106
Class for multi-channel digital images. Supports dynamic typing with base types (char,...
Definition Image.h:193
Definition DataSelector.cpp:1277