1 /*
2 Copyright (c) 2014-2021 Timur Gafarov, Martin Cejp
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 /**
30  * Load and save images
31  *
32  * Copyright: Timur Gafarov, Martin Cejp 2014-2021.
33  * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0).
34  * Authors: Timur Gafarov, Martin Cejp
35  */
36 module dlib.image.io;
37 
38 import std.path;
39 import dlib.image.image;
40 import dlib.image.animation;
41 import dlib.image.hdri;
42 
43 public
44 {
45     import dlib.image.io.bmp;
46     import dlib.image.io.hdr;
47     import dlib.image.io.png;
48     import dlib.image.io.tga;
49     import dlib.image.io.jpeg;
50 }
51 
52 class ImageLoadException : Exception
53 {
54     this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)
55     {
56         super(msg, file, line, next);
57     }
58 }
59 
60 /**
61  * Saves an image to file, selects encoder by filename extension
62  */
63 void saveImage(SuperImage img, string filename)
64 {
65     switch(filename.extension)
66     {
67         case ".png", ".PNG":
68             img.savePNG(filename);
69             break;
70         case ".bmp", ".BMP":
71             img.saveBMP(filename);
72             break;
73         case ".tga", ".TGA":
74             img.saveTGA(filename);
75             break;
76         default:
77             assert(0, "Image I/O error: unsupported image format or illegal extension");
78     }
79 }
80 
81 /**
82  * Loads an image from file, selects decoder by filename extension
83  */
84 SuperImage loadImage(string filename)
85 {
86     switch(filename.extension)
87     {
88         case ".bmp", ".BMP":
89             return loadBMP(filename);
90         case ".hdr", ".HDR":
91             return loadHDR(filename);
92         case ".jpg", ".JPG", ".jpeg", ".JPEG":
93             return loadJPEG(filename);
94         case ".png", ".PNG":
95             return loadPNG(filename);
96         case ".tga", ".TGA":
97             return loadTGA(filename);
98         default:
99             assert(0, "Image I/O error: unsupported image format or illegal extension");
100     }
101 }
102 
103 /**
104  * Loads an animated image from file, selects decoder by filename extension
105  */
106 SuperAnimatedImage loadAnimatedImage(string filename)
107 {
108     switch(filename.extension)
109     {
110         case ".png", ".apng", ".PNG", ".APNG":
111             return loadAPNG(filename);
112         default:
113             assert(0, "Image I/O error: unsupported image format or illegal extension");
114     }
115 }
116 
117 /**
118  * Saves an animated image to file, selects encoder by filename extension
119  */
120 void saveAnimatedImage(SuperAnimatedImage img, string filename)
121 {
122     switch(filename.extension)
123     {
124         case ".png", ".PNG", ".apng", ".APNG":
125             img.saveAPNG(filename);
126             break;
127         default:
128             assert(0, "Image I/O error: unsupported image format or illegal extension");
129     }
130 }
131 
132 /**
133  * Loads an HDR image from file, selects decoder by filename extension
134  */
135 SuperImage loadHDRImage(string filename)
136 {
137     switch(filename.extension)
138     {
139         case ".hdr", ".HDR":
140             return loadHDR(filename);
141         default:
142             assert(0, "Image I/O error: unsupported image format or illegal extension");
143     }
144 }
145 
146 /**
147  * Saves an HDR to file, selects encoder by filename extension
148  */
149 void saveHDRImage(SuperHDRImage img, string filename)
150 {
151     switch(filename.extension)
152     {
153         case ".hdr", ".HDR":
154             img.saveHDR(filename);
155             break;
156         default:
157             assert(0, "Image I/O error: unsupported image format or illegal extension");
158     }
159 }