1 #pragma once
2 
3 #ifndef TIMAGE_IO_INCLUDED
4 #define TIMAGE_IO_INCLUDED
5 
6 //#include "trasterimage.h"
7 //#include "texception.h"
8 //#include "tfilepath.h"
9 #include <QStringList>
10 
11 #include "tfilepath_io.h"
12 #include "traster.h"
13 #include "timage.h"
14 
15 #undef DVAPI
16 #undef DVVAR
17 #ifdef TIMAGE_IO_EXPORTS
18 #define DVAPI DV_EXPORT_API
19 #define DVVAR DV_EXPORT_VAR
20 #else
21 #define DVAPI DV_IMPORT_API
22 #define DVVAR DV_IMPORT_VAR
23 #endif
24 
25 // forward declaration
26 namespace Tiio {
27 class Reader;
28 class Writer;
29 class VectorReader;
30 class VectorWriter;
31 }
32 class TPropertyGroup;
33 class TImageInfo;
34 
35 //===========================================================
36 
37 class DVAPI TImageException final : public TException {
38   TFilePath m_fp;
39 
40 public:
41   TImageException(const TFilePath &fp, const std::string &msg);
~TImageException()42   ~TImageException() {}
43 
44   TString getMessage() const override;
getRawMessage()45   TString getRawMessage() const { return TException::getMessage(); }
getFilePath()46   const TFilePath &getFilePath() const { return m_fp; }
47 };
48 
49 //===========================================================
50 
51 class DVAPI TImageVersionException final : public TException {
52   TFilePath m_fp;
53   int m_major, m_minor;
54 
55 public:
56   TImageVersionException(const TFilePath &fp, int major, int minor);
~TImageVersionException()57   ~TImageVersionException() {}
58 
getVersion(int & major,int & minor)59   void getVersion(int &major, int &minor) const {
60     major = m_major;
61     minor = m_minor;
62   }
getMajor()63   int getMajor() const { return m_major; }
getMinor()64   int getMinor() const { return m_minor; }
65 };
66 
67 //===========================================================
68 //
69 // Image Reader
70 //
71 //===========================================================
72 
73 class TImageReader;
74 typedef TImageReader *TImageReaderCreateProc(const TFilePath &path);
75 
76 //===========================================================
77 
78 //! \include imgReader_ex.cpp
79 
80 class DVAPI TImageReader : public TSmartObject {
81   DECLARE_CLASS_CODE
82 
83   friend class TImageReaderP;
84 
85 protected:
86   // std::fstream m_stream;
87   TFilePath m_path;
88   FILE *m_file;
89   Tiio::Reader *m_reader;
90   Tiio::VectorReader *m_vectorReader;
91   void open();
92   void close();
93   bool isOpen() const;
94   bool m_readGreytones;
95   bool m_is64BitEnabled;
96   int m_shrink;
97   TRect m_region;
98   static bool m_safeMode;
99 
100 public:
setSafeModeReadingForTzl(bool activated)101   static void setSafeModeReadingForTzl(bool activated) {
102     m_safeMode = activated;
103   }
104   TImageReader(const TFilePath &path);
105   virtual ~TImageReader();
106 
107 private:
108   // not implemented
109   TImageReader(const TImageReader &);
110   TImageReader &operator=(const TImageReader &src);
111   TImageP load0();
112 
113 public:
114   // TImageReader keeps ownership: DO NOT DELETE
115   virtual const TImageInfo *getImageInfo() const;
116 
getFilePath()117   TFilePath getFilePath() const { return m_path; };
118 
119   TPropertyGroup *getProperties();
120   void setProperties(const TPropertyGroup *);
121   /*! Load Image from disk.
122 If set region then loads only image's region.
123           If set shrink then loads image (or region, if set) with shrink.
124 
125 Note: if the region, or part of it, is not contained in the image
126     then returns only the intersection between the image and region.
127           If the intersection is void returns TImageP();
128 */
129   virtual TImageP load();
130 
131   void load(const TRasterP &ras, const TPoint &pos = TPoint(0, 0),
132             int shrinkX = 1, int shrinkY = 1);
133 
134   static bool load(const TFilePath &, TRasterP &);
135   static bool load(const TFilePath &, TImageP &);
136 
137   // Only for TLV (version 13). Return the icon in TLV file.
loadIcon()138   virtual TImageP loadIcon() { return TImageP(); }
139 
140   static void getSupportedFormats(QStringList &names);
141 
142   // TDimension getSize() const;
143   // virtual TRect getBBox() const = 0;
144 
145   static void define(QString extension, TImageReaderCreateProc *proc);
146 
147   void doReadGraytones(bool readThem);
enable16BitRead(bool is64bitEnabled)148   void enable16BitRead(bool is64bitEnabled) {
149     m_is64BitEnabled = is64bitEnabled;
150   }
151 
getShrink()152   int getShrink() const { return m_shrink; }
153   /*!
154      Setta lo shrink in modo che al load() viene letta da disco
155            una riga ogni "shrink" righe ed all'interno di queste righe viene
156      letto un pixel ogni "shrink" pixel.
157            La prima riga viene sempre letta.
158            Il primo pixel di una riga letta viene sempre preso.
159      Nota che se shrink=1 non viene saltata alcuna riga ne pixel.
160   */
161   void setShrink(int shrink);
162 
163   /*!
164 Set image's region.
165 Region dimension doesn't consider shrink.
166 */
setRegion(TRect rect)167   void setRegion(TRect rect) { m_region = rect; }
168   /*!
169 Returns the image region.
170 Region dimension doesn't consider shrink
171 */
getRegion()172   TRect getRegion() const { return m_region; }
173 
174   void getTzpPaletteColorNames(
175       std::map<int, std::pair<std::string, std::string>>
176           &pltColorNames);  // colorindex(<256: paint), pagename, colorname
177 };
178 
179 //-----------------------------------------------------------
180 
181 #ifdef _WIN32
182 template class DVAPI TSmartPointerT<TImageReader>;
183 #endif
184 
185 class DVAPI TImageReaderP final : public TSmartPointerT<TImageReader> {
186 public:
TImageReaderP(TImageReader * ir)187   TImageReaderP(TImageReader *ir) : TSmartPointerT<TImageReader>(ir){};
188   // il costruttore "non banale"
189   TImageReaderP(const TFilePath &filepath);
190 };
191 
192 //===========================================================
193 //
194 // Image Writer
195 //
196 //===========================================================
197 
198 class TImageWriter;
199 typedef TImageWriter *TImageWriterCreateProc(const TFilePath &path);
200 
201 //===========================================================
202 
203 //! \include imgWriter_ex.cpp
204 
205 class DVAPI TImageWriter : public TSmartObject {
206   DECLARE_CLASS_CODE
207 
208   // Background color for saving transparent pixel to the format not
209   // supporting alpha channel. Specified in the preferences.
210   static TPixel32 m_backgroundColor;
211 
212 protected:
213   // std::fstream m_stream;
214   TFilePath m_path;
215 
216   Tiio::Writer *m_writer;
217   Tiio::VectorWriter *m_vectorWriter;
218   TPropertyGroup *m_properties;
219 
220 public:
221   TImageWriter(const TFilePath &path);
222   virtual ~TImageWriter();
223 
224 private:
225   // not implemented
226   TImageWriter(const TImageWriter &);
227   TImageWriter &operator=(const TImageWriter &src);
228 
229 public:
getFilePath()230   TFilePath getFilePath() const { return m_path; };
231 
232   // don't get ownership
233   void setProperties(const TPropertyGroup *);
234 
235   virtual void save(const TImageP &img);
is64bitOutputSupported()236   virtual bool is64bitOutputSupported() { return true; }
237   static void save(const TFilePath &, TRasterP);
238   static void save(const TFilePath &, const TImageP &);
239 
240   static void getSupportedFormats(QStringList &names, bool onlyRenderFormats);
241 
242   static void define(QString extension, TImageWriterCreateProc *proc,
243                      bool isRenderFormat);
244 
245   static void setBackgroundColor(TPixel32 color);
246   static TPixel32 getBackgroundColor();
247 };
248 
249 //-----------------------------------------------------------
250 
251 #ifdef _WIN32
252 template class DVAPI TSmartPointerT<TImageWriter>;
253 #endif
254 
255 class DVAPI TImageWriterP final : public TSmartPointerT<TImageWriter> {
256 public:
TImageWriterP(TImageWriter * iw)257   TImageWriterP(TImageWriter *iw) : TSmartPointerT<TImageWriter>(iw){};
258   // il costruttore "non banale"
259   TImageWriterP(const TFilePath &filepath);
260 };
261 
262 //===========================================================
263 //
264 // Image ReaderWriter
265 //
266 //===========================================================
267 
268 class TImageReaderWriter;
269 typedef TImageReaderWriter *TImageReaderWriterCreateProc(const TFilePath &path);
270 
271 //===========================================================
272 
273 #endif
274