GCC Code Coverage Report


Directory: ./
File: image/image_serializer_stb.cpp
Date: 2026-10-17 02:40:23
Exec Total Coverage
Lines: 13 56 23.2%
Functions: 5 11 45.5%
Branches: 0 62 0.0%

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
41 #include <geogram/image/image_serializer_stb.h>
42 #include <geogram/image/image_library.h>
43 #include <geogram/basic/file_system.h>
44 #include <geogram/basic/string.h>
45 #include <geogram/basic/logger.h>
46
47 #define STB_IMAGE_IMPLEMENTATION
48 #define STB_IMAGE_WRITE_IMPLEMENTATION
49
50 // Use internal linkage for symbols from stb_image as it is a very
51 // commonly embedded library.
52 // Making these symbols visible causes duplicate symbol problems
53 // if geogram is linked statically together with another library or
54 // executable that also embeds stb_image.
55 #define STB_IMAGE_STATIC
56 #define STB_IMAGE_WRITE_STATIC
57
58 // [Bruno] I got too many complaints in STB so I "close my eyes" :-)
59 #ifdef __GNUC__
60 #ifndef __ICC
61 #pragma GCC diagnostic ignored "-Wpragmas"
62 #pragma GCC diagnostic ignored "-Wconversion"
63 #pragma GCC diagnostic ignored "-Wfloat-conversion"
64 #pragma GCC diagnostic ignored "-Wsign-conversion"
65 #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
66 #pragma GCC diagnostic ignored "-Wdouble-promotion"
67 #pragma GCC diagnostic ignored "-Wunused-function"
68 #ifndef __clang__
69 #pragma GCC diagnostic ignored "-Wstringop-overflow"
70 #endif
71 #endif
72
73 #ifdef __clang__
74 #pragma GCC diagnostic ignored "-Wdisabled-macro-expansion"
75 #pragma GCC diagnostic ignored "-Wextra-semi-stmt"
76 #pragma GCC diagnostic ignored "-Wcast-align"
77 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
78 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
79 #pragma GCC diagnostic ignored "-Wreserved-id-macro"
80 #pragma GCC diagnostic ignored "-Wcomma"
81 #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
82 #pragma GCC diagnostic ignored "-Wcast-qual"
83 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
84 #pragma GCC diagnostic ignored "-Wreserved-identifier"
85 #endif
86 #endif
87
88 #ifdef _MSC_VER
89 #pragma warning( disable : 4505 )
90 #pragma warning( disable : 4244 )
91 #endif
92
93 #ifdef GEOGRAM_USE_BUILTIN_DEPS
94 #include <geogram/third_party/stb_image/stb_image.h>
95 #include <geogram/third_party/stb_image/stb_image_write.h>
96 #else
97 #include <stb/stb_image.h>
98 #include <stb/stb_image_write.h>
99 #endif
100
101 namespace GEO {
102
103 1275 ImageSerializerSTB::ImageSerializerSTB(bool read, bool write) :
104 1275 read_(read),
105 1275 write_(write)
106 {
107 1275 }
108
109 ✗ Image* ImageSerializerSTB::serialize_read(const std::string& file_name) {
110 std::string extension = String::to_lowercase(
111 ✗ FileSystem::extension(file_name)
112 ✗ );
113 if(
114 ✗ extension != "png" &&
115 ✗ extension != "jpg" &&
116 ✗ extension != "jpeg" &&
117 ✗ extension != "tga" &&
118 extension != "bmp"
119 ) {
120 return nullptr;
121 }
122
123 Image* result = nullptr;
124 int width, height, bpp;
125 int desired_bpp = 4;
126 ✗ unsigned char* data = stbi_load(
127 file_name.c_str(), &width, &height, &bpp, desired_bpp
128 );
129 ✗ if(data == nullptr) {
130 ✗ Logger::err("Image")
131 << file_name << " : could not load file." << std::endl;
132 } else {
133 result = new Image(
134 Image::RGBA,
135 Image::BYTE, index_t(width), index_t(height)
136 ✗ );
137 // bpp: how many bytes per pixel there was in the file
138 // desired_bpp set to 4, thus stbi expands it to r,g,b,a always.
139 ✗ Memory::copy(result->base_mem(), data, size_t(width*height*4));
140 stbi_image_free(data);
141 // Seems that STB has inverse convention as mine for image Y axis,
142 ✗ result->flip_vertically();
143 }
144
145 return result;
146 }
147
148 ✗ bool ImageSerializerSTB::serialize_write(
149 const std::string& file_name, const Image* image_in
150 ) {
151 bool result = true;
152 std::string extension = String::to_lowercase(
153 ✗ FileSystem::extension(file_name)
154 ✗ );
155
156 // Seems that STB has inverse convention as mine for image Y axis,
157 // so we flip before saving and flip back after.
158
159 Image* image = const_cast<Image*>(image_in);
160 ✗ image->flip_vertically();
161
162 ✗ int w = int(image->width());
163 ✗ int h = int(image->height());
164 ✗ int comp = int(image->components_per_pixel());
165 const char* data = (const char*)(image->base_mem());
166
167 ✗ if(extension == "png") {
168 ✗ result = (
169 ✗ stbi_write_png(file_name.c_str(), w, h, comp, data, w*comp) != 0
170 );
171 ✗ } else if(extension == "bmp") {
172 ✗ result = (
173 ✗ stbi_write_bmp(file_name.c_str(), w, h, comp, data) != 0
174 );
175 ✗ } else if(extension == "jpeg" || extension == "jpg") {
176 ✗ result = (
177 ✗ stbi_write_jpg(file_name.c_str(), w, h, comp, data, 80) != 0
178 );
179 ✗ } else if(extension == "tga") {
180 ✗ result = (
181 ✗ stbi_write_tga(file_name.c_str(), w, h, comp, data) != 0
182 );
183 }
184
185 ✗ image->flip_vertically();
186 ✗ return result;
187 }
188
189 ✗ bool ImageSerializerSTB::binary() const {
190 ✗ return true;
191 }
192
193 ✗ bool ImageSerializerSTB::streams_supported() const {
194 ✗ return false;
195 }
196
197 6375 bool ImageSerializerSTB::read_supported() const {
198 6375 return read_;
199 }
200
201 6375 bool ImageSerializerSTB::write_supported() const {
202 6375 return write_;
203 }
204
205 /*****************************************************/
206
207 ✗ ImageSerializerSTBRead::ImageSerializerSTBRead() :
208 ✗ ImageSerializerSTB(true, false) {
209 ✗ }
210
211 ✗ ImageSerializerSTBRead::~ImageSerializerSTBRead() {
212 ✗ }
213
214 /*****************************************************/
215
216 1275 ImageSerializerSTBReadWrite::ImageSerializerSTBReadWrite() :
217 1275 ImageSerializerSTB(true, true) {
218 1275 }
219
220 5100 ImageSerializerSTBReadWrite::~ImageSerializerSTBReadWrite() {
221 5100 }
222
223 /*****************************************************/
224
225 }
226