Debian Codegraph @

cvd/image_io.h

raw symbols SCIP
1 #ifndef CVD_IMAGE_IO_H
2 #define CVD_IMAGE_IO_H
3
4 #include <cctype>
5 #include <cvd/convert_image.h>
6 #include <cvd/exceptions.h>
7 #include <cvd/internal/load_and_save.h>
8 #include <cvd/internal/name_CVD_rgb_types.h>
9 #include <cvd/internal/name_builtin_types.h>
10 #include <errno.h>
11 #include <fstream>
12 #include <memory>
13 #include <string>
14 #include <type_traits>
15
16 #include <cvd/internal/io/bmp.h>
17 #include <cvd/internal/io/cvdimage.h>
18 #include <cvd/internal/io/fits.h>
19 #include <cvd/internal/io/jpeg.h>
20 #include <cvd/internal/io/png.h>
21 #include <cvd/internal/io/pnm_grok.h>
22 #include <cvd/internal/io/save_postscript.h>
23 #include <cvd/internal/io/text.h>
24 #include <cvd/internal/io/tiff.h>
25
26 namespace CVD
27 {
28
29 // This is not the real definition, but this is what it would look like if all
30 // the macros were expanded. The real definition is above
31 /// Contains the enumeration of possible image types
32 namespace ImageType
33 {
34 /// Possible image types
35 enum ImageType
36 {
37 /// Placeholder type telling @ref save_image to deduce the type from the filename
38 Automatic = -2,
39 /// Unknown image type (can be returned by @ref string_to_image_type
40 Unknown = -1,
41 /// PNM image format (PBM, PGM or PPM).
42 /// This writes 8 or 16 bit raw PGMs or PPMs. PBM is not currently supported
43 PNM = 0,
44 /// JPEG image format. This is a compressed (lossy) image format, but defaults to 95% quality, which has very few compression artefacts. This image type is only present if libjpeg is available.
45 /// RGB and Greyscale JPEGs are supported
46 JPEG = 1,
47 /// Windows BMP (or DIB) format. Uncompressed 8 bit grey scale and 24 bit RGB are supported.
48 BMP = 2,
49 /// PNG image format. 1, 8 and 16 bit, Greyscale, RGB and RGBA images are supported.
50 /// This image type is only present if libpng is available.
51 PNG = 3,
52 /// TIFF image format. 1, 8, 16, 32 (float) and 64 (double) suported. Greyscale, RGB and RGBA supported.
53 /// This image type is only present if libtiff is available. G4 FAX encoding is used for bools, otherwise
54 /// "Deflate" compression is used.
55 TIFF = 4,
56 /// Postscript format. This outputs a bare PostScript image with the coordinate system set up
57 /// to that (x,y) corresponds to pixel (x,y), with (0,0) being at the top left of the pixel (0,0).
58 /// The Y axis is therefore inverted compared to normal postscript drawing, but is image aligned.
59 /// To get the drawing axes aligned with the centre of the pixels, write the postscript command
60 /// ".5 .5 translate" after the image.
61 /// The image data in encoded in ASCII-85 for portability. Helper functions are provided for
62 /// generating EPS figures. See CVD::output_eps_header and CVD::output_eps_footer
63 PS = 5,
64 /// EPS format. This outputs a complete EPS (Encapsulated PostScript) figure.
65 EPS = 6,
66 /// Plain text format. Grey-scale floating point only. This can be read in to MATLAB
67 /// with the load() function. There is no metadata, so it is not possible to support
68 /// multiple types.
69 TXT = 7,
70 TEXT = 7,
71 /// CVD image format. 8 bit Grey-scale, RGB and RGBA
72 /// images are currently supported. Files of this type
73 /// have a simple PNM-like header, but use a lossless
74 /// compression scheme (line-wise prediction + Huffman).
75 CVD = 8,
76 /// FITS images. Supports (native) byte, short, unsigned short, int, float and double
77 /// of greyscale, RGB and RGBA. Signed char is supported lossley but inefficiently.
78 FITS = 9,
79 };
80 }
81
82 #if DOXYGEN_INCLUDE_ONLY_FOR_DOCS
83
84 /// Load an image from a stream.
85 ///
86 /// @param i The istream to load from
87 /// @ingroup gImageIO
88 template <class C>
89 Image<C> img_load(std::istream& i);
90
91 /// This function resizes the Image as necessary.
92 /// It will also perform image type conversion (e.g. colour to greyscale)
93 /// according the Pixel:::CIE conversion.
94 /// The template type is deduced automatically, and must not be specified.
95 ///
96 /// The type deduction is performed using lazy evaluation, so the load operation
97 /// is only performed if an image is assigned from this.
98 /// Load an image from a file, and return the image.
99 ///
100 /// You can also restrict the range of supported images if you wish to omit certain
101 /// image types from the final binary
102 ///
103 /// \code
104 /// Image<byte> i = img_load("file"); //Load a file, all types supported.
105 /// Image<byte> i = img_load<PNG::Reader, JPEG::Reader>("file"); //Load only supporting JPEG and PNG
106 ///
107 /// using Files = std::tuple<PNG::Reader, JPEG::Reader>;
108 /// Image<byte> i = img_load<Files>("file"); //Load only supporting JPEG and PNG
109 ///
110 /// \endcode
111 ///
112 /// The type deduction is performed using lazy evaluation, so the load operation
113 /// is only performed if an image is assigned from this.
114 ///
115 /// @param i The istream to load from
116 /// @ingroup gImageIO
117 template <class C>
118 Image<C> img_load(const std::string& i);
119
120 #endif
121
122 namespace Internal
123 {
124
125 using AllImageTypes = std::tuple<PNM::Reader, JPEG::Reader, TIFF::Reader, PNG::Reader, BMP::Reader, FITS::Reader, CVDimage::Reader, TEXT::Reader>;
126
127 // This selects the correct image reader from the list of available readers
128 // (provided as a tuple), using the first byte of the file to decide.
129 template <class I, class ImageTypeList, int N = 0>
130 void img_load_tuple(Image<I>& im, std::istream& i, [[maybe_unused]] int c)
131 {
132 if constexpr(N == std::tuple_size_v<ImageTypeList>)
133 {
134 throw Exceptions::Image_IO::UnsupportedImageType();
135 }
136 else
137 {
138 using ImageReader = std::tuple_element_t<N, ImageTypeList>;
139
140 if(ImageReader::first_byte_matches(c))
141 CVD::Internal::readImage<I, ImageReader>(im, i);
142 else
143 img_load_tuple<I, ImageTypeList, N + 1>(im, i, c);
144 }
145 }
146
147 template <class... T>
148 struct as_tuple
149 {
150 using type = std::tuple<T...>;
151 };
152
153 template <class... T>
154 struct as_tuple<std::tuple<T...>>
155 {
156 using type = std::tuple<T...>;
157 };
158 }
159
160 //If there's only one argument it can be a tuple or a single element typelist
161 template <class I, class Head = Internal::AllImageTypes, class... ImageTypes>
162 void img_load(Image<I>& im, std::istream& i)
163 {
164 if(!i.good())
165 {
166 //Check for one of the commonest errors and put in
167 //a special case
168 std::ifstream* fs;
169 if((fs = dynamic_cast<std::ifstream*>(&i)) && !fs->is_open())
170 throw Exceptions::Image_IO::IfstreamNotOpen();
171 else
172 throw Exceptions::Image_IO::EofBeforeImage();
173 }
174 int c = i.peek();
175
176 if(!i.good())
177 throw Exceptions::Image_IO::EofBeforeImage();
178
179 Internal::img_load_tuple<I, typename Internal::as_tuple<Head, ImageTypes...>::type>(im, i, c);
180 }
181
182 template <class I, class Head = Internal::AllImageTypes, class... ImageTypes>
183 void img_load(Image<I>& im, const std::string& s)
184 {
185 std::ifstream i(s.c_str(), std::ios::in | std::ios::binary);
186
187 if(!i.good())
188 throw Exceptions::Image_IO::OpenError(s, "for reading", errno);
189 img_load<I, Head, ImageTypes...>(im, i);
190 }
191
192 #ifndef DOXYGEN_IGNORE_INTERNAL
193 namespace Internal
194 {
195 template <class... ImageTypes>
196 class ImageLoaderIstream
197 {
198 };
199
200 template <class... ImageTypes>
201 struct ImagePromise<ImageLoaderIstream<ImageTypes...>>
202 {
203 ImagePromise(std::istream& is)
204 : i(is)
205 {
206 }
207
208 std::istream& i;
209 template <class C>
210 void execute(Image<C>& im)
211 {
212 img_load<C, ImageTypes...>(im, i);
213 }
214 };
215
216 template <class... ImageTypes>
217 class ImageLoaderString
218 {
219 };
220
221 template <class... ImageTypes>
222 struct ImagePromise<ImageLoaderString<ImageTypes...>>
223 {
224 ImagePromise(const std::string& ss)
225 : s(ss)
226 {
227 }
228
229 const std::string& s;
230 template <class C>
231 void execute(Image<C>& im)
232 {
233 img_load<C, ImageTypes...>(im, s);
234 }
235 };
236
237 };
238
239 template <class... ImageTypes>
240 Internal::ImagePromise<Internal::ImageLoaderIstream<ImageTypes...>> img_load(std::istream& i)
241 {
242 return i;
243 }
244
245 template <class... ImageTypes>
246 Internal::ImagePromise<Internal::ImageLoaderString<ImageTypes...>> img_load(const std::string& i)
247 {
248 return i;
249 }
250
251 inline Internal::ImagePromise<Internal::ImageLoaderIstream<Internal::AllImageTypes>> img_load(std::istream& i)
252 {
253 return i;
254 }
255
256 inline Internal::ImagePromise<Internal::ImageLoaderString<Internal::AllImageTypes>> img_load(const std::string& i)
257 {
258 return i;
259 }
260
261 #endif
262
263 ////////////////////////////////////////////////////////////////////////////////
264 //
265 // Image saving
266 //
267
268 /// Deduce an image type from a filename suffix.
269 /// @param name The name of the image file
270 ImageType::ImageType string_to_image_type(const std::string& name);
271
272 /// Save an image to a stream. This function will convert types if necessary.
273 /// @param PixelType The pixel type of the image
274 /// @param Conversion The conversion class to use
275 /// @param im The image to save
276 /// @param o The stream
277 /// @param t The image file format to use (see ImageType::ImageType for a list of supported formats)
278 /// @ingroup gImageIO
279 template <class PixelType>
280 void img_save(const BasicImage<PixelType>& im, std::ostream& o, ImageType::ImageType t, const std::map<std::string, Parameter<>>& p = std::map<std::string, Parameter<>>())
281 {
282 switch(t)
283 {
284 default:
285 case ImageType::PNM:
286 case ImageType::Automatic:
287 case ImageType::Unknown:
288 Internal::writeImage<PixelType, PNM::Writer>(im, o, p);
289 break;
290 case ImageType::JPEG:
291 Internal::writeImage<PixelType, JPEG::writer>(im, o, p);
292 break;
293 case ImageType::PNG:
294 Internal::writeImage<PixelType, PNG::png_writer>(im, o, p);
295 break;
296 case ImageType::TIFF:
297 Internal::writeImage<PixelType, TIFF::tiff_writer>(im, o, p);
298 break;
299 case ImageType::FITS:
300 Internal::writeImage<PixelType, FITS::writer>(im, o, p);
301 break;
302 case ImageType::BMP:
303 Internal::writeImage<PixelType, BMP::Writer>(im, o, p);
304 break;
305 case ImageType::TXT:
306 Internal::writeImage<PixelType, TEXT::writer>(im, o, p);
307 break;
308 case ImageType::PS:
309 Internal::writeImage<PixelType, PS::writer>(im, o, p);
310 break;
311 case ImageType::EPS:
312 Internal::writeImage<PixelType, PS::eps_writer>(im, o, p);
313 break;
314 case ImageType::CVD:
315 Internal::writeImage<PixelType, CVDimage::writer>(im, o, p);
316 break;
317 }
318 }
319
320 template <class PixelType>
321 void img_save(const BasicImage<PixelType>& im, const std::string& name, ImageType::ImageType t, ImageType::ImageType d = ImageType::PNM, const std::map<std::string, Parameter<>>& p = std::map<std::string, Parameter<>>())
322 {
323 std::ofstream out(name.c_str(), std::ios::out | std::ios::binary);
324 if(!out.good())
325 throw Exceptions::Image_IO::OpenError(name, "for writing", errno);
326
327 if(t == ImageType::Automatic)
328 {
329 t = string_to_image_type(name);
330 if(t == ImageType::Unknown)
331 t = d;
332 }
333
334 img_save(im, out, t, p);
335 }
336
337 template <class PixelType>
338 void img_save(const BasicImage<PixelType>& im, const std::string& name, const std::map<std::string, Parameter<>>& p = std::map<std::string, Parameter<>>())
339 {
340 img_save(im, name, ImageType::Automatic, ImageType::PNM, p);
341 }
342
343 ////////////////////////////////////////////////////////////////////////////////
344 //
345 // Legacy pnm_* functions
346 //
347
348 /// Save an image to a stream as a PNM.
349 /// @b Deprecated Use img_save() instead, i.e. <code>img_save(im, o, ImageType::PNM);</code>
350 /// @param PixelType The pixel type of the image
351 /// @param im The image
352 /// @param o The stream
353 /// @ingroup gImageIO
354 template <class PixelType>
355 void pnm_save(const BasicImage<PixelType>& im, std::ostream& o)
356 {
357 img_save(im, o, ImageType::PNM);
358 }
359
360 /// Load a PNM image from a stream.
361 /// @b Deprecated Use img_load(Image<I>& im, std::istream& i) instead. This can handle
362 /// and automatically detect other file formats as well.
363 ///
364 /// Loading is simplistic, and automatic conversions of values are performed.
365 /// So, for instance bytes are assumed to be in the range 0--255 and floats in
366 /// the range 0--1, so loading bytes in to an Image<float> will result in an
367 /// image with floats in the range 0--1. In the reverse direction,
368 /// floats falling outside the range 0--1, will wrap when converted to bytes.
369 ///
370 /// The image loader ignores the data range given by the file, if it is given.
371 ///
372 /// @param PixelType The pixel type of the image
373 /// @param im The image
374 /// @param i The stream
375 /// @ingroup gImageIO
376 template <class PixelType>
377 void pnm_load(Image<PixelType>& im, std::istream& i)
378 {
379 img_load(im, i);
380 }
381
382 ////////////////////////////////////////////////////////////////////////////////
383 //
384 // Postscript helper functions
385 //
386
387 /// Outputs an EPS footer to an ostream
388 /// @param o the ostream
389 /// @ingroup gImageIO
390 void output_eps_footer(std::ostream& o);
391
392 /// Outputs an EPS header to an ostream. Typical use is to output the header,
393 /// save a raw PS image, output some other PS (eg annotations) and the output
394 /// the EPS footer.
395 /// @param o the ostream
396 /// @param xs the width of the image
397 /// @param ys the height of the image
398 /// @ingroup gImageIO
399 void output_eps_header(std::ostream& o, int xs, int ys);
400
401 /// Outputs an EPS header to an ostream. Typical use is to output the header,
402 /// save a raw PS image, output some other PS (eg annotations) and the output
403 /// the EPS footer.
404 /// @param o the ostream
405 /// @param s size of the image
406 /// @ingroup gImageIO
407 void output_eps_header(std::ostream& o, const ImageRef& s);
408
409 /// Outputs an EPS header to an ostream. Typical use is to output the header,
410 /// save a raw PS image, output some other PS (eg annotations) and the output
411 /// the EPS footer.
412 /// @param o the ostream
413 /// @param im the image
414 /// @ingroup gImageIO
415 template <class PixelType>
416 void output_eps_header(std::ostream& o, const BasicImage<PixelType>& im)
417 {
418 output_eps_header(o, im.size());
419 }
420
421 }
422
423 #endif