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  // Weak
67 
68  static
69  const dict_t & getCompressionDict();
70 
71  // https://www.awaresystems.be/imaging/tiff/tifftags/compression.html
72  // https://gdal.org/drivers/raster/cog.html
73  static dict_t::value_t defaultCompression; // COMPRESSION_NONE = 1; COMPRESSION_LZW = 5;
74  // static int defaultCompressionLevel; // COMPRESSION_NONE = 1; COMPRESSION_LZW = 5;
75 
76  static drain::Frame2D<int> defaultTile;
77 
78 
79 #ifndef USE_GEOTIFF_NO
80 
81 
82  inline
83  FileTIFF(const std::string & path = "", const std::string & mode = "w") : tif(nullptr), tile(defaultTile){ // FileHandler(__FUNCTION__),
84  if (!path.empty())
85  open(path, mode);
86  //tif = XTIFFOpen(path.c_str(), mode);
87  }
88 
89  virtual inline
90  ~FileTIFF(){
91  //mout.special("Closing FileTIFF");
92  close();
93  }
94 
95  virtual inline
96  void open(const std::string & path, const std::string & mode = "w"){
97  tif = XTIFFOpen(path.c_str(), mode.c_str());
98  }
99 
100  virtual inline
101  bool isOpen() const {
102  return (tif != nullptr);
103  }
104 
105  virtual inline
106  void close(){
107  if (isOpen()){
108  drain::Logger mout(__FILE__, __FUNCTION__);
109  mout.debug("Closing TIFF...");
110  XTIFFClose(tif);
111  tif = nullptr;
112  }
113  }
114 
115  inline
116  int setField(int tag, const std::string & value){
117  if (!isOpen()){
118  drain::Logger mout(__FILE__, __FUNCTION__);
119  mout.error("TIFF file not open");
120  }
121  return TIFFSetField(tif, tag, value.c_str());
122  }
123 
124  template <class T>
125  inline
126  int setField(int tag, const std::vector<T> & value){
127  if (!isOpen()){
128  drain::Logger mout(__FILE__, __FUNCTION__);
129  mout.error("TIFF file not open");
130  //return 0;
131  }
132  return TIFFSetField(tif, tag, value.size(), &value.at(0));
133  }
134 
135  template <class T>
136  inline
137  int setField(int tag, T value){
138  if (!isOpen()){
139  drain::Logger mout(__FILE__, __FUNCTION__);
140  mout.error("TIFF file not open");
141  //return 0;
142  }
143  return TIFFSetField(tif, tag, value);
144  }
145 
146 
147  inline
148  void useDefaultTileSize(){
149  this->tile = defaultTile;
150  }
151 
152  inline
153  void setTileSize(int tileWidth, int tileHeight = 0){
154  if (tileWidth == 0){
155  tile = {0,0};
156  //this->tile = defaultTile;
157  }
158  else {
159  tile.setWidth(tileWidth);
160  if (tileHeight == 0){
161  tileHeight = tileWidth;
162  }
163  tile.setHeight(tileHeight);
164  }
165  }
166 
169  void setTime(const drain::Time & time);
170 
174  void setDefaults(); //, int tileWidth=0, int tileHeight = 0);
175  //void setDefaults(const drain::image::ImageConf & src); //, int tileWidth=0, int tileHeight = 0);
176 
177  void writeImageData(const drain::image::Image & src);
178 
180 
183  static
184  void write(const std::string & path, const drain::image::Image & src);
185 
186 
187 protected:
188 
189  TIFF *tif;
190 
191  drain::Frame2D<int> tile;
192 
193 
194 #endif
195 
196 };
197 
198 } // image::
199 
200 } // drain::
201 
202 
203 
204 #endif
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:308
Logger & error(const TT &... args)
Echoes.
Definition: Log.h:412
Logger & debug(const TT &... args)
Public, yet typically used "internally", when TIMING=true.
Definition: Log.h:676
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:303
void writeImageData(const drain::image::Image &src)
Definition: FileTIFF.cpp:114
Class for multi-channel digital images. Supports dynamic typing with base types (char,...
Definition: Image.h:184
Definition: DataSelector.cpp:1277