GCC Code Coverage Report


Directory: ./
File: lib/geogram/image/image_library.h
Date: 2026-09-07 02:37:58
Exec Total Coverage
Lines: 4 4 100.0%
Functions: 3 3 100.0%
Branches: 3 8 37.5%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40 #ifndef H_OGF_IMAGE_TYPES_IMAGE_LIBRARY_H
41 #define H_OGF_IMAGE_TYPES_IMAGE_LIBRARY_H
42
43 #include <geogram/basic/common.h>
44 #include <geogram/image/image.h>
45 #include <geogram/image/image_serializer.h>
46
47 #include <geogram/basic/environment.h>
48
49 #include <string>
50 #include <map>
51
52 /**
53 * \file geogram/image/image_library.h
54 * \brief Management of image serializers and named images.
55 */
56
57 namespace GEO {
58
59 //_________________________________________________________
60
61 class ImageSerializer;
62 class Image;
63
64 /**
65 * \brief Manages the ImageSerializer repository and
66 * the named images.
67 */
68 class GEOGRAM_API ImageLibrary : public Environment {
69 public:
70
71 /**
72 * \brief Gets the instance.
73 * \return a pointer to the unique instance of ImageLibrary.
74 */
75 static ImageLibrary* instance();
76
77 /**
78 * \brief Initializes the ImageLibrary instance.
79 * \details This function is automatically called during
80 * Geogram startup. It should not be called by client
81 * code.
82 */
83 static void initialize();
84
85 /**
86 * \brief Terminates the ImageLibrary instance.
87 * \details This function is automatically called during
88 * Geogram shutdown. It should not be called by client
89 * code.
90 */
91 static void terminate();
92
93 /**
94 * \brief Binds an ImageSerializer.
95 * \param[in] extension the file extension without the "."
96 * \param[in] serializer a pointer to an ImageSerializer. Ownership
97 * is transferred to this ImageLibrary
98 * \retval true if the ImageSerializer could be successfully bound
99 * \retval false otherwise (i.e. if there was already a serializer
100 * with the same name).
101 */
102 bool bind_image_serializer(
103 const std::string& extension, ImageSerializer* serializer
104 );
105
106 /**
107 * \brief Finds an ImageSerializer by extension.
108 * \param[in] extension the file extension without the "."
109 * \return a pointer to the ImageSerializer that can serialize
110 * images in the file format that corresponds to the extension,
111 * or nil if no such ImageSerializer was found
112 */
113 ImageSerializer* resolve_image_serializer(
114 const std::string& extension
115 ) const;
116
117 /**
118 * \brief Binds an image with a name.
119 * \param[in] name the name of the image
120 * \param[in] image a pointer to the image to be bound. Ownership
121 * is transferred to this ImageLibrary.
122 * \retval true if the Image could be successfully bound
123 * \retval false otherwise, i.e. if there was already an Image
124 * bound with the same name
125 */
126 bool bind_image(const std::string& name, Image* image);
127
128 /**
129 * \brief Unbinds a named image.
130 * \param[in] name the name of the image
131 * \retval true if the Image could be successfully unbound
132 * \retval false otherwise, i.e. if there was no Image
133 * bound with the specified name
134 */
135 bool unbind_image(const std::string& name);
136
137 /**
138 * \brief Finds an image by name.
139 * \param[in] name the name of the image
140 * \return a pointer to the image, or nil if no image
141 * is bound to the name
142 * \see bind_image(), unbind_image()
143 */
144 Image* resolve_image(const std::string& name) const;
145
146 /**
147 * \brief Loads an image from a file.
148 * \param[in] file_name the name of the file that contains the image
149 * \return a pointer to the loaded image, or nil if the image could
150 * not be loaded.
151 * \note The returned pointer can be stored in an Image_var
152 */
153 Image* load_image(const std::string& file_name);
154
155 /**
156 * \brief Saves an image into a file.
157 * \param[in] file_name the name of the file that will receive the
158 * image
159 * \param[in] image a pointer to the Image to be saved
160 * \retval true if the image could be successfully saved
161 * \retval false otherwise
162 */
163 bool save_image(const std::string& file_name, Image* image);
164
165 /**
166 * \brief Copies an image to the clipboard of the operating system.
167 * \param[in] image the image to be copied to the clipboard.
168 * \note Only implemented under Windows
169 */
170 void copy_image_to_clipboard(Image* image);
171
172 /**
173 * \copydoc Environment::get_local_value()
174 * \details Provides the following environment variables:
175 * - image_read_extensions
176 * - image_write_extensions
177 */
178 bool get_local_value(
179 const std::string& name, std::string& value
180 ) const override;
181
182 /**
183 * \copydoc Environment::set_local_value()
184 */
185 bool set_local_value(
186 const std::string& name, const std::string& value
187 ) override;
188
189 protected:
190 ImageLibrary();
191 ~ImageLibrary() override;
192 friend class World;
193
194 private:
195 static ImageLibrary* instance_;
196 std::map<std::string, ImageSerializer_var> image_serializers_;
197 std::map<std::string, Image_var> images_;
198 };
199
200 //_________________________________________________________
201
202 /**
203 * \brief Declares an image serializer for a given extension.
204 * \tparam T the type of the ImageSerializer
205 */
206 template <class T> class geo_declare_image_serializer {
207 public:
208 /**
209 * \brief Declares an image serializer for a given extension.
210 * \param[in] extension the extension of the image file names
211 * without the "."
212 * \details This function is supposed to be used in the
213 * initializers of the libraries. An example of usage:
214 * \code
215 * ogf_declare_image_serializer<ImageSerializer_png>("png");
216 * \endcode
217 */
218 3514 geo_declare_image_serializer(const std::string& extension) {
219
2/6
✓ Branch 2 taken 1757 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1757 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
7028 ImageLibrary::instance()->bind_image_serializer(
220
1/2
✓ Branch 2 taken 1255 times.
✗ Branch 3 not taken.
3514 extension, new T
221 );
222 3514 }
223 };
224
225 //_________________________________________________________
226
227 }
228 #endif
229