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