Fixed an issue with an endless loop when attempting to load a corrupt image file.

This commit is contained in:
Leon Styhre 2021-07-02 20:33:50 +02:00
parent 506a452d1b
commit 651b7a4d02
5 changed files with 29 additions and 3 deletions

View file

@ -426,7 +426,8 @@ void ImageComponent::render(const Transform4x4f& parentTrans)
mTargetSize.y(), 0xFF000033, 0xFF000033); mTargetSize.y(), 0xFF000033, 0xFF000033);
Renderer::drawRect(0.0f, 0.0f, mSize.x(), mSize.y(), 0xFF000033, 0xFF000033); Renderer::drawRect(0.0f, 0.0f, mSize.x(), mSize.y(), 0xFF000033, 0xFF000033);
} }
if (mTexture->isInitialized()) { // An image with zero size would normally indicate a corrupt image file.
if (mTexture->isInitialized() && mTexture->getSize() != 0) {
// Actually draw the image. // Actually draw the image.
// The bind() function returns false if the texture is not currently loaded. A blank // The bind() function returns false if the texture is not currently loaded. A blank
// texture is bound in this case but we want to handle a fade so it doesn't just // texture is bound in this case but we want to handle a fade so it doesn't just
@ -441,7 +442,19 @@ void ImageComponent::render(const Transform4x4f& parentTrans)
Renderer::drawTriangleStrips(&mVertices[0], 4, trans); Renderer::drawTriangleStrips(&mVertices[0], 4, trans);
} }
else { else {
LOG(LogError) << "Image texture is not initialized!"; if (!mTexture) {
LOG(LogError) << "Image texture is not initialized";
}
else {
std::string textureFilePath = mTexture->getTextureFilePath();
if (textureFilePath != "") {
LOG(LogError) << "Image texture for file \"" << textureFilePath <<
"\" has zero size";
}
else {
LOG(LogError) << "Image texture has zero size";
}
}
mTexture.reset(); mTexture.reset();
} }
} }

View file

@ -124,7 +124,7 @@ bool TextureData::initImageFromMemory(const unsigned char* fileData, size_t leng
if (imageRGBA.size() == 0) { if (imageRGBA.size() == 0) {
LOG(LogError) << "Couldn't initialize texture from memory, invalid data (" << LOG(LogError) << "Couldn't initialize texture from memory, invalid data (" <<
(mPath != "" ? "file path: " + mPath + ", " : "") << "data ptr: " << (mPath != "" ? "file path: \"" + mPath + "\", " : "") << "data ptr: " <<
reinterpret_cast<size_t>(fileData) << ", reported size: " << length << ")"; reinterpret_cast<size_t>(fileData) << ", reported size: " << length << ")";
return false; return false;
} }

View file

@ -58,6 +58,7 @@ public:
void setScaleDuringLoad(float scale) { mScaleDuringLoad = scale; } void setScaleDuringLoad(float scale) { mScaleDuringLoad = scale; }
std::vector<unsigned char> getRawRGBAData() { return mDataRGBA; } std::vector<unsigned char> getRawRGBAData() { return mDataRGBA; }
std::string getTextureFilePath() { return mPath; }
bool tiled() { return mTile; } bool tiled() { return mTile; }
private: private:

View file

@ -115,6 +115,16 @@ std::vector<unsigned char> TextureResource::getRawRGBAData()
return std::vector<unsigned char>(0); return std::vector<unsigned char>(0);
} }
std::string TextureResource::getTextureFilePath()
{
std::shared_ptr<TextureData> data = sTextureDataManager.get(this);
if (data)
return data->getTextureFilePath();
else
return "";
}
const Vector2i TextureResource::getSize() const const Vector2i TextureResource::getSize() const
{ {
return mSize; return mSize;

View file

@ -39,6 +39,8 @@ public:
// Returns the raw pixel values. // Returns the raw pixel values.
std::vector<unsigned char> getRawRGBAData(); std::vector<unsigned char> getRawRGBAData();
std::string getTextureFilePath();
// For SVG graphics this function effectively rescales the image to the defined size. // For SVG graphics this function effectively rescales the image to the defined size.
// It does unload and re-rasterize the texture though which may cause flickering in some // It does unload and re-rasterize the texture though which may cause flickering in some
// situations. An alternative is to set a scaling factor directly when loading the texture // situations. An alternative is to set a scaling factor directly when loading the texture