From 651b7a4d022a48d39d9f8865dfd3f42e17491cc5 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Fri, 2 Jul 2021 20:33:50 +0200 Subject: [PATCH] Fixed an issue with an endless loop when attempting to load a corrupt image file. --- es-core/src/components/ImageComponent.cpp | 17 +++++++++++++++-- es-core/src/resources/TextureData.cpp | 2 +- es-core/src/resources/TextureData.h | 1 + es-core/src/resources/TextureResource.cpp | 10 ++++++++++ es-core/src/resources/TextureResource.h | 2 ++ 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/es-core/src/components/ImageComponent.cpp b/es-core/src/components/ImageComponent.cpp index 90eda6801..b53059b27 100644 --- a/es-core/src/components/ImageComponent.cpp +++ b/es-core/src/components/ImageComponent.cpp @@ -426,7 +426,8 @@ void ImageComponent::render(const Transform4x4f& parentTrans) mTargetSize.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. // 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 @@ -441,7 +442,19 @@ void ImageComponent::render(const Transform4x4f& parentTrans) Renderer::drawTriangleStrips(&mVertices[0], 4, trans); } 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(); } } diff --git a/es-core/src/resources/TextureData.cpp b/es-core/src/resources/TextureData.cpp index 1f8b020cd..c43298804 100644 --- a/es-core/src/resources/TextureData.cpp +++ b/es-core/src/resources/TextureData.cpp @@ -124,7 +124,7 @@ bool TextureData::initImageFromMemory(const unsigned char* fileData, size_t leng if (imageRGBA.size() == 0) { LOG(LogError) << "Couldn't initialize texture from memory, invalid data (" << - (mPath != "" ? "file path: " + mPath + ", " : "") << "data ptr: " << + (mPath != "" ? "file path: \"" + mPath + "\", " : "") << "data ptr: " << reinterpret_cast(fileData) << ", reported size: " << length << ")"; return false; } diff --git a/es-core/src/resources/TextureData.h b/es-core/src/resources/TextureData.h index ca7e52973..8a65c0b18 100644 --- a/es-core/src/resources/TextureData.h +++ b/es-core/src/resources/TextureData.h @@ -58,6 +58,7 @@ public: void setScaleDuringLoad(float scale) { mScaleDuringLoad = scale; } std::vector getRawRGBAData() { return mDataRGBA; } + std::string getTextureFilePath() { return mPath; } bool tiled() { return mTile; } private: diff --git a/es-core/src/resources/TextureResource.cpp b/es-core/src/resources/TextureResource.cpp index 887d6d408..5aef84b3f 100644 --- a/es-core/src/resources/TextureResource.cpp +++ b/es-core/src/resources/TextureResource.cpp @@ -115,6 +115,16 @@ std::vector TextureResource::getRawRGBAData() return std::vector(0); } +std::string TextureResource::getTextureFilePath() +{ + std::shared_ptr data = sTextureDataManager.get(this); + + if (data) + return data->getTextureFilePath(); + else + return ""; +} + const Vector2i TextureResource::getSize() const { return mSize; diff --git a/es-core/src/resources/TextureResource.h b/es-core/src/resources/TextureResource.h index 4db29adde..380be1787 100644 --- a/es-core/src/resources/TextureResource.h +++ b/es-core/src/resources/TextureResource.h @@ -39,6 +39,8 @@ public: // Returns the raw pixel values. std::vector getRawRGBAData(); + std::string getTextureFilePath(); + // 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 // situations. An alternative is to set a scaling factor directly when loading the texture