| 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 "-Wcast-align" | ||
| 76 | #pragma GCC diagnostic ignored "-Wmissing-prototypes" | ||
| 77 | #pragma GCC diagnostic ignored "-Wunknown-pragmas" | ||
| 78 | #pragma GCC diagnostic ignored "-Wreserved-id-macro" | ||
| 79 | #pragma GCC diagnostic ignored "-Wcomma" | ||
| 80 | #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" | ||
| 81 | #pragma GCC diagnostic ignored "-Wcast-qual" | ||
| 82 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | ||
| 83 | #endif | ||
| 84 | #endif | ||
| 85 | |||
| 86 | #ifdef _MSC_VER | ||
| 87 | #pragma warning( disable : 4505 ) | ||
| 88 | #pragma warning( disable : 4244 ) | ||
| 89 | #endif | ||
| 90 | |||
| 91 | #ifdef GEOGRAM_USE_BUILTIN_DEPS | ||
| 92 | #include <geogram/third_party/stb_image/stb_image.h> | ||
| 93 | #include <geogram/third_party/stb_image/stb_image_write.h> | ||
| 94 | #else | ||
| 95 | #include <stb/stb_image.h> | ||
| 96 | #include <stb/stb_image_write.h> | ||
| 97 | #endif | ||
| 98 | |||
| 99 | namespace GEO { | ||
| 100 | |||
| 101 | 1245 | ImageSerializerSTB::ImageSerializerSTB(bool read, bool write) : | |
| 102 | 1245 | read_(read), | |
| 103 | 1245 | write_(write) | |
| 104 | { | ||
| 105 | 1245 | } | |
| 106 | |||
| 107 | ✗ | Image* ImageSerializerSTB::serialize_read(const std::string& file_name) { | |
| 108 | std::string extension = String::to_lowercase( | ||
| 109 | ✗ | FileSystem::extension(file_name) | |
| 110 | ✗ | ); | |
| 111 | if( | ||
| 112 | ✗ | extension != "png" && | |
| 113 | ✗ | extension != "jpg" && | |
| 114 | ✗ | extension != "jpeg" && | |
| 115 | ✗ | extension != "tga" && | |
| 116 | extension != "bmp" | ||
| 117 | ) { | ||
| 118 | return nullptr; | ||
| 119 | } | ||
| 120 | |||
| 121 | Image* result = nullptr; | ||
| 122 | int width, height, bpp; | ||
| 123 | int desired_bpp = 4; | ||
| 124 | ✗ | unsigned char* data = stbi_load( | |
| 125 | file_name.c_str(), &width, &height, &bpp, desired_bpp | ||
| 126 | ); | ||
| 127 | ✗ | if(data == nullptr) { | |
| 128 | ✗ | Logger::err("Image") | |
| 129 | << file_name << " : could not load file." << std::endl; | ||
| 130 | } else { | ||
| 131 | result = new Image( | ||
| 132 | Image::RGBA, | ||
| 133 | Image::BYTE, index_t(width), index_t(height) | ||
| 134 | ✗ | ); | |
| 135 | // bpp: how many bytes per pixel there was in the file | ||
| 136 | // desired_bpp set to 4, thus stbi expands it to r,g,b,a always. | ||
| 137 | ✗ | Memory::copy(result->base_mem(), data, size_t(width*height*4)); | |
| 138 | stbi_image_free(data); | ||
| 139 | // Seems that STB has inverse convention as mine for image Y axis, | ||
| 140 | ✗ | result->flip_vertically(); | |
| 141 | } | ||
| 142 | |||
| 143 | return result; | ||
| 144 | } | ||
| 145 | |||
| 146 | ✗ | bool ImageSerializerSTB::serialize_write( | |
| 147 | const std::string& file_name, const Image* image_in | ||
| 148 | ) { | ||
| 149 | bool result = true; | ||
| 150 | std::string extension = String::to_lowercase( | ||
| 151 | ✗ | FileSystem::extension(file_name) | |
| 152 | ✗ | ); | |
| 153 | |||
| 154 | // Seems that STB has inverse convention as mine for image Y axis, | ||
| 155 | // so we flip before saving and flip back after. | ||
| 156 | |||
| 157 | Image* image = const_cast<Image*>(image_in); | ||
| 158 | ✗ | image->flip_vertically(); | |
| 159 | |||
| 160 | ✗ | int w = int(image->width()); | |
| 161 | ✗ | int h = int(image->height()); | |
| 162 | ✗ | int comp = int(image->components_per_pixel()); | |
| 163 | const char* data = (const char*)(image->base_mem()); | ||
| 164 | |||
| 165 | ✗ | if(extension == "png") { | |
| 166 | ✗ | result = ( | |
| 167 | ✗ | stbi_write_png(file_name.c_str(), w, h, comp, data, w*comp) != 0 | |
| 168 | ); | ||
| 169 | ✗ | } else if(extension == "bmp") { | |
| 170 | ✗ | result = ( | |
| 171 | ✗ | stbi_write_bmp(file_name.c_str(), w, h, comp, data) != 0 | |
| 172 | ); | ||
| 173 | ✗ | } else if(extension == "jpeg" || extension == "jpg") { | |
| 174 | ✗ | result = ( | |
| 175 | ✗ | stbi_write_jpg(file_name.c_str(), w, h, comp, data, 80) != 0 | |
| 176 | ); | ||
| 177 | ✗ | } else if(extension == "tga") { | |
| 178 | ✗ | result = ( | |
| 179 | ✗ | stbi_write_tga(file_name.c_str(), w, h, comp, data) != 0 | |
| 180 | ); | ||
| 181 | } | ||
| 182 | |||
| 183 | ✗ | image->flip_vertically(); | |
| 184 | ✗ | return result; | |
| 185 | } | ||
| 186 | |||
| 187 | ✗ | bool ImageSerializerSTB::binary() const { | |
| 188 | ✗ | return true; | |
| 189 | } | ||
| 190 | |||
| 191 | ✗ | bool ImageSerializerSTB::streams_supported() const { | |
| 192 | ✗ | return false; | |
| 193 | } | ||
| 194 | |||
| 195 | 6225 | bool ImageSerializerSTB::read_supported() const { | |
| 196 | 6225 | return read_; | |
| 197 | } | ||
| 198 | |||
| 199 | 6225 | bool ImageSerializerSTB::write_supported() const { | |
| 200 | 6225 | return write_; | |
| 201 | } | ||
| 202 | |||
| 203 | /*****************************************************/ | ||
| 204 | |||
| 205 | ✗ | ImageSerializerSTBRead::ImageSerializerSTBRead() : | |
| 206 | ✗ | ImageSerializerSTB(true, false) { | |
| 207 | ✗ | } | |
| 208 | |||
| 209 | ✗ | ImageSerializerSTBRead::~ImageSerializerSTBRead() { | |
| 210 | ✗ | } | |
| 211 | |||
| 212 | /*****************************************************/ | ||
| 213 | |||
| 214 | 1245 | ImageSerializerSTBReadWrite::ImageSerializerSTBReadWrite() : | |
| 215 | 1245 | ImageSerializerSTB(true, true) { | |
| 216 | 1245 | } | |
| 217 | |||
| 218 | 4980 | ImageSerializerSTBReadWrite::~ImageSerializerSTBReadWrite() { | |
| 219 | 4980 | } | |
| 220 | |||
| 221 | /*****************************************************/ | ||
| 222 | |||
| 223 | } | ||
| 224 |