#ifndef GUL_STB_IMAGE_LOAD_H #define GUL_STB_IMAGE_LOAD_H #include #include #include // C++17 includes the library, but // unfortunately gcc7 does not have a finalized version of it // it is in the ) #include namespace gul { namespace fs = std::filesystem; } #elif __has_include() #include namespace gul { namespace fs = std::experimental::filesystem; } #else #error There is no or #endif namespace gul { inline gul::Image loadImage(fs::path const & p, int desiredChannels=4) { int x,y,n; unsigned char *data = stbi_load(p.c_str(), &x, &y, &n, desiredChannels); gul::Image I( static_cast(x), static_cast(y), static_cast(desiredChannels) ); std::memcpy(I.data(), data, I.byteSize()); stbi_image_free(data); return I; } inline gul::Image loadImage(void const* data, int len, int desiredChannels=4) { int x,y,channels; auto imgData = stbi_load_from_memory( static_cast(data), len, &x, &y, &channels, desiredChannels); gul::Image I( static_cast(x), static_cast(y), static_cast(desiredChannels) ); std::memcpy(I.data(), imgData, I.byteSize()); stbi_image_free(imgData); return I; } } #endif