stb: exposed error messages
This commit is contained in:
@@ -25,6 +25,8 @@
|
||||
#define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL
|
||||
#define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM
|
||||
|
||||
#define STBI_FAILURE_USERMSG
|
||||
|
||||
#include <stb_image.h>
|
||||
#include <stb_image_resize.h>
|
||||
|
||||
@@ -44,8 +46,7 @@ namespace Image
|
||||
return std::make_unique<STB::RawImage>(path);
|
||||
}
|
||||
|
||||
void
|
||||
init(const std::filesystem::path&)
|
||||
void init(const std::filesystem::path&)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -55,21 +56,20 @@ namespace Image::STB
|
||||
RawImage::RawImage(const std::byte* encodedData, std::size_t encodedDataSize)
|
||||
{
|
||||
int n;
|
||||
_data = UniquePtrFree {stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(encodedData), encodedDataSize, &_width, &_height, &n, 3), std::free};
|
||||
_data = UniquePtrFree{ ::stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(encodedData), encodedDataSize, &_width, &_height, &n, 3), std::free };
|
||||
if (!_data)
|
||||
throw ImageException {"Cannot load image from memory"};
|
||||
throw ImageException{ "Cannot load image from memory: " + std::string{ ::stbi_failure_reason() } };
|
||||
}
|
||||
|
||||
RawImage::RawImage(const std::filesystem::path& p)
|
||||
{
|
||||
int n;
|
||||
_data = UniquePtrFree {stbi_load(p.string().c_str(), &_width, &_height, &n, 3), std::free};
|
||||
_data = UniquePtrFree{ stbi_load(p.string().c_str(), &_width, &_height, &n, 3), std::free };
|
||||
if (!_data)
|
||||
throw ImageException {"Cannot load image from memory"};
|
||||
throw ImageException{ "Cannot load image from file: " + std::string{ ::stbi_failure_reason() } };
|
||||
}
|
||||
|
||||
void
|
||||
RawImage::resize(ImageSize width)
|
||||
void RawImage::resize(ImageSize width)
|
||||
{
|
||||
size_t height;
|
||||
if (_width == _height)
|
||||
@@ -78,23 +78,23 @@ namespace Image::STB
|
||||
}
|
||||
else if (_width > _height)
|
||||
{
|
||||
height = (size_t)((float)width/_width*_height);
|
||||
height = (size_t)((float)width / _width * _height);
|
||||
}
|
||||
else
|
||||
{
|
||||
height = width;
|
||||
width = (size_t)((float)height/_height*_width);
|
||||
width = (size_t)((float)height / _height * _width);
|
||||
}
|
||||
|
||||
UniquePtrFree resizedData {reinterpret_cast<unsigned char*>(malloc(width*height*3)), std::free};
|
||||
UniquePtrFree resizedData{ reinterpret_cast<unsigned char*>(malloc(width * height * 3)), std::free };
|
||||
if (!resizedData)
|
||||
throw ImageException {"Cannot allocate memory for resized image!"};
|
||||
throw ImageException{ "Cannot allocate memory for resized image!" };
|
||||
|
||||
if (stbir_resize_uint8_srgb(reinterpret_cast<const unsigned char*>(_data.get()), _width, _height, 0,
|
||||
if (::stbir_resize_uint8_srgb(reinterpret_cast<const unsigned char*>(_data.get()), _width, _height, 0,
|
||||
reinterpret_cast<unsigned char*>(resizedData.get()), width, height, 0,
|
||||
3, STBIR_ALPHA_CHANNEL_NONE, 0) == 0)
|
||||
{
|
||||
throw ImageException {"Failed to resize image!"};
|
||||
throw ImageException{ "Failed to resize image:" + std::string{ ::stbi_failure_reason() } };
|
||||
}
|
||||
|
||||
_data = std::move(resizedData);
|
||||
@@ -102,26 +102,22 @@ namespace Image::STB
|
||||
_width = width;
|
||||
}
|
||||
|
||||
std::unique_ptr<IEncodedImage>
|
||||
RawImage::encodeToJPEG(unsigned quality) const
|
||||
std::unique_ptr<IEncodedImage> RawImage::encodeToJPEG(unsigned quality) const
|
||||
{
|
||||
return std::make_unique<JPEGImage>(*this, quality);
|
||||
}
|
||||
|
||||
ImageSize
|
||||
RawImage::getWidth() const
|
||||
ImageSize RawImage::getWidth() const
|
||||
{
|
||||
return _width;
|
||||
}
|
||||
|
||||
ImageSize
|
||||
RawImage::getHeight() const
|
||||
ImageSize RawImage::getHeight() const
|
||||
{
|
||||
return _height;
|
||||
}
|
||||
|
||||
const std::byte*
|
||||
RawImage::getData() const
|
||||
const std::byte* RawImage::getData() const
|
||||
{
|
||||
if (!_data)
|
||||
return nullptr;
|
||||
@@ -129,4 +125,3 @@ namespace Image::STB
|
||||
return reinterpret_cast<const std::byte*>(_data.get());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace Image::STB
|
||||
int _width;
|
||||
int _height;
|
||||
using UniquePtrFree = std::unique_ptr<unsigned char, decltype(&std::free)>;
|
||||
UniquePtrFree _data {nullptr, std::free};
|
||||
UniquePtrFree _data{ nullptr, std::free };
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user