implement getting image size without reading the whole image
This commit is contained in:
@@ -60,4 +60,10 @@ namespace OpenVulkano::Image
|
|||||||
stbi_image_free(pixelData);
|
stbi_image_free(pixelData);
|
||||||
return std::make_unique<Image>(std::move(result));
|
return std::make_unique<Image>(std::move(result));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool IImageLoader::GetDimensionsInternal(const std::string& filename, int& width, int& height)
|
||||||
|
{
|
||||||
|
int channels;
|
||||||
|
return stbi_info(filename.c_str(), &width, &height, &channels);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -21,5 +21,8 @@ namespace OpenVulkano::Image
|
|||||||
static std::unique_ptr<Image> loadData(const uint8_t* data, int size, int desiredChannels = 0);
|
static std::unique_ptr<Image> loadData(const uint8_t* data, int size, int desiredChannels = 0);
|
||||||
virtual std::unique_ptr<Image> loadFromFile(const std::string& filePath) = 0;
|
virtual std::unique_ptr<Image> loadFromFile(const std::string& filePath) = 0;
|
||||||
virtual std::unique_ptr<Image> loadFromMemory(const std::vector<uint8_t>& buffer) = 0;
|
virtual std::unique_ptr<Image> loadFromMemory(const std::vector<uint8_t>& buffer) = 0;
|
||||||
|
virtual bool GetImageDimensions(const std::string& filename, int& width, int& height) = 0;
|
||||||
|
protected:
|
||||||
|
static bool GetDimensionsInternal(const std::string& filename, int& width, int& height);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -31,6 +31,32 @@ namespace OpenVulkano::Image
|
|||||||
return loadJpeg(buffer.data(), buffer.size());
|
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)
|
std::unique_ptr<Image> ImageLoaderJpeg::loadJpeg(const uint8_t* data, size_t size)
|
||||||
{
|
{
|
||||||
#if __has_include("turbojpeg.h")
|
#if __has_include("turbojpeg.h")
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace OpenVulkano::Image
|
|||||||
public:
|
public:
|
||||||
std::unique_ptr<Image> loadFromFile(const std::string& filePath) override;
|
std::unique_ptr<Image> loadFromFile(const std::string& filePath) override;
|
||||||
std::unique_ptr<Image> loadFromMemory(const std::vector<uint8_t>& buffer) override;
|
std::unique_ptr<Image> loadFromMemory(const std::vector<uint8_t>& buffer) override;
|
||||||
|
bool GetImageDimensions(const std::string& filename, int& width, int& height) override;
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Image> loadJpeg(const uint8_t* data, size_t size);
|
std::unique_ptr<Image> loadJpeg(const uint8_t* data, size_t size);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -22,4 +22,9 @@ namespace OpenVulkano::Image
|
|||||||
{
|
{
|
||||||
return loadData(buffer.data(), static_cast<int>(buffer.size()));
|
return loadData(buffer.data(), static_cast<int>(buffer.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ImageLoaderPng::GetImageDimensions(const std::string& filename, int& width, int& height)
|
||||||
|
{
|
||||||
|
return GetDimensionsInternal(filename, width, height);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -15,5 +15,6 @@ namespace OpenVulkano::Image
|
|||||||
public:
|
public:
|
||||||
std::unique_ptr<Image> loadFromFile(const std::string& filePath) override;
|
std::unique_ptr<Image> loadFromFile(const std::string& filePath) override;
|
||||||
std::unique_ptr<Image> loadFromMemory(const std::vector<uint8_t>& buffer) override;
|
std::unique_ptr<Image> loadFromMemory(const std::vector<uint8_t>& buffer) override;
|
||||||
|
bool GetImageDimensions(const std::string& filename, int& width, int& height) override;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user