implement getting image size without reading the whole image

This commit is contained in:
ohyzha
2024-10-07 17:18:30 +03:00
parent c00e8a69e2
commit a4e716006a
6 changed files with 42 additions and 1 deletions

View File

@@ -31,6 +31,32 @@ namespace OpenVulkano::Image
return loadJpeg(buffer.data(), buffer.size());
}
bool ImageLoaderJpeg::GetImageDimensions(const std::string& filename, int& width, int& height)
{
#if __has_include("turbojpeg.h")
auto image = Utils::ReadFile(filename);
tjhandle jpegDecompressor = tjInitDecompress();
if (!jpegDecompressor)
{
Logger::FILESYS->error("Failed to read jpeg header. Error: {}", tjGetErrorStr());
return false;
}
int size = 0, jpegSubsamp = 0;
int status = tjDecompressHeader2(jpegDecompressor, reinterpret_cast<unsigned char*>(image.Data()), image.Size(),
&width, &height, &jpegSubsamp);
if (status != 0)
{
Logger::FILESYS->error("Failed to read jpeg header. Error: {}", tjGetErrorStr());
tjDestroy(jpegDecompressor);
return false;
}
tjDestroy(jpegDecompressor);
return true;
#else
return IImageLoader::GetDimensionsInternal(filename, width, height);
#endif
}
std::unique_ptr<Image> ImageLoaderJpeg::loadJpeg(const uint8_t* data, size_t size)
{
#if __has_include("turbojpeg.h")