diff options
author | Dave Blake <oak99sky@yahoo.co.uk> | 2020-10-20 22:30:39 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-20 22:30:39 +0100 |
commit | f48d89080661eba495a045566946b7f49f41272a (patch) | |
tree | 1e2223926966b278a3bf474f6699697fd6ec162c | |
parent | 89b58608f04aac99161d3cf87fde4fe5145ff5ea (diff) | |
parent | 5dc076aa62f508161f0fb29193389695b518fd02 (diff) |
Merge pull request #18579 from basilgello/simplify-texturepacker-leia
[BACKPORT][Leia]TexturePacker: Do not leak image data memory
7 files changed, 66 insertions, 61 deletions
diff --git a/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.cpp b/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.cpp index 7e5179b0cc..d7c5e2ac55 100644 --- a/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.cpp +++ b/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.cpp @@ -31,6 +31,8 @@ bool GIFDecoder::CanDecode(const std::string &filename) bool GIFDecoder::LoadFile(const std::string &filename, DecodedFrames &frames) { int n = 0; + bool result = false; + GifHelper *gifImage = new GifHelper(); if (gifImage->LoadGif(filename.c_str())) { @@ -53,28 +55,20 @@ bool GIFDecoder::LoadFile(const std::string &filename, DecodedFrames &frames) frame.rgbaImage.bbp = 32; frame.rgbaImage.pitch = pitch; frame.delay = extractedFrames[i]->m_delay; + frame.decoder = this; frames.frameList.push_back(frame); } } - frames.user = gifImage; - return true; - } - else - { - delete gifImage; - return false; + result = true; } + delete gifImage; + return result; } -void GIFDecoder::FreeDecodedFrames(DecodedFrames &frames) +void GIFDecoder::FreeDecodedFrame(DecodedFrame &frame) { - for (unsigned int i = 0; i < frames.frameList.size(); i++) - { - delete [] frames.frameList[i].rgbaImage.pixels; - } - delete (GifHelper *)frames.user; - frames.clear(); + delete [] frame.rgbaImage.pixels; } void GIFDecoder::FillSupportedExtensions() diff --git a/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.h b/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.h index 6e9ea6c4be..8f0b42d822 100644 --- a/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.h +++ b/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.h @@ -28,7 +28,7 @@ class GIFDecoder : public IDecoder ~GIFDecoder() override = default; bool CanDecode(const std::string &filename) override; bool LoadFile(const std::string &filename, DecodedFrames &frames) override; - void FreeDecodedFrames(DecodedFrames &frames) override; + void FreeDecodedFrame(DecodedFrame &frame) override; const char* GetImageFormatName() override { return "GIF"; } const char* GetDecoderName() override { return "libgif"; } protected: diff --git a/tools/depends/native/TexturePacker/src/decoder/IDecoder.h b/tools/depends/native/TexturePacker/src/decoder/IDecoder.h index b197296d9b..28243acbf1 100644 --- a/tools/depends/native/TexturePacker/src/decoder/IDecoder.h +++ b/tools/depends/native/TexturePacker/src/decoder/IDecoder.h @@ -23,6 +23,34 @@ #include <string> #include <vector> +/* forward declarations */ + +class DecodedFrame; +class DecodedFrames; + +class IDecoder +{ + public: + virtual ~IDecoder() = default; + virtual bool CanDecode(const std::string &filename) = 0; + virtual bool LoadFile(const std::string &filename, DecodedFrames &frames) = 0; + virtual void FreeDecodedFrame(DecodedFrame &frame) = 0; + virtual const char* GetImageFormatName() = 0; + virtual const char* GetDecoderName() = 0; + + const std::vector<std::string>& GetSupportedExtensions() + { + m_supportedExtensions.clear(); + FillSupportedExtensions(); + return m_supportedExtensions; + } + + protected: + virtual void FillSupportedExtensions() = 0; + //fill this with extensions in FillSupportedExtensions like ".png" + std::vector<std::string> m_supportedExtensions; +}; + class RGBAImage { public: @@ -39,43 +67,32 @@ class DecodedFrame { public: DecodedFrame() : delay(0) { } - RGBAImage rgbaImage; /* rgbaimage for this frame */ - int delay; /* Frame delay in ms */ + RGBAImage rgbaImage; /* rgbaimage for this frame */ + int delay = 0; /* Frame delay in ms */ + IDecoder* decoder = nullptr; /* Pointer to decoder */ }; class DecodedFrames { public: - DecodedFrames(): user(NULL) {} + DecodedFrames() = default; std::vector<DecodedFrame> frameList; - void *user; /* used internally*/ void clear() { + for (auto f : frameList) + { + if (f.decoder != NULL) + { + f.decoder->FreeDecodedFrame(f); + } + else + { + fprintf(stderr, + "ERROR: %s - can not determine decoder type for frame!\n", + __FUNCTION__); + } + } frameList.clear(); - user = NULL; } }; - -class IDecoder -{ - public: - virtual ~IDecoder() = default; - virtual bool CanDecode(const std::string &filename) = 0; - virtual bool LoadFile(const std::string &filename, DecodedFrames &frames) = 0; - virtual void FreeDecodedFrames(DecodedFrames &frames) = 0; - virtual const char* GetImageFormatName() = 0; - virtual const char* GetDecoderName() = 0; - - const std::vector<std::string>& GetSupportedExtensions() - { - m_supportedExtensions.clear(); - FillSupportedExtensions(); - return m_supportedExtensions; - } - - protected: - virtual void FillSupportedExtensions() = 0; - //fill this with extensions in FillSupportedExtensions like ".png" - std::vector<std::string> m_supportedExtensions; -}; diff --git a/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.cpp b/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.cpp index 4022dc719f..15a646f95b 100644 --- a/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.cpp +++ b/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.cpp @@ -84,7 +84,6 @@ bool JPGDecoder::LoadFile(const std::string &filename, DecodedFrames &frames) // Image Size is calculated as (width * height * bytes per pixel = 4 ImageSize = cinfo.image_width * cinfo.image_height * 4; - frames.user = NULL; DecodedFrame frame; frame.rgbaImage.pixels = (char *)new char[ImageSize]; @@ -115,20 +114,18 @@ bool JPGDecoder::LoadFile(const std::string &filename, DecodedFrames &frames) frame.rgbaImage.width = cinfo.image_width; frame.rgbaImage.bbp = 32; frame.rgbaImage.pitch = 4 * cinfo.image_width; + + frame.decoder = this; + frames.frameList.push_back(frame); delete arq; return true; } -void JPGDecoder::FreeDecodedFrames(DecodedFrames &frames) +void JPGDecoder::FreeDecodedFrame(DecodedFrame &frame) { - for (unsigned int i = 0; i < frames.frameList.size(); i++) - { - delete [] frames.frameList[i].rgbaImage.pixels; - } - - frames.clear(); + delete [] frame.rgbaImage.pixels; } void JPGDecoder::FillSupportedExtensions() diff --git a/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.h b/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.h index 30430e79f9..bbf23ba7b3 100644 --- a/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.h +++ b/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.h @@ -28,7 +28,7 @@ class JPGDecoder : public IDecoder ~JPGDecoder() override = default; bool CanDecode(const std::string &filename) override; bool LoadFile(const std::string &filename, DecodedFrames &frames) override; - void FreeDecodedFrames(DecodedFrames &frames) override; + void FreeDecodedFrame(DecodedFrame &frame) override; const char* GetImageFormatName() override { return "JPG"; } const char* GetDecoderName() override { return "libjpeg"; } protected: diff --git a/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.cpp b/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.cpp index 96ca0b461f..e5ca91b5c6 100644 --- a/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.cpp +++ b/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.cpp @@ -214,7 +214,6 @@ bool PNGDecoder::LoadFile(const std::string &filename, DecodedFrames &frames) // read the png into image_data through row_pointers png_read_image(png_ptr, row_pointers); - frames.user = NULL; DecodedFrame frame; frame.rgbaImage.pixels = (char *)image_data; @@ -222,6 +221,9 @@ bool PNGDecoder::LoadFile(const std::string &filename, DecodedFrames &frames) frame.rgbaImage.width = temp_width; frame.rgbaImage.bbp = 32; frame.rgbaImage.pitch = 4 * temp_width; + + frame.decoder = this; + frames.frameList.push_back(frame); // clean up png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); @@ -229,14 +231,9 @@ bool PNGDecoder::LoadFile(const std::string &filename, DecodedFrames &frames) return true; } -void PNGDecoder::FreeDecodedFrames(DecodedFrames &frames) +void PNGDecoder::FreeDecodedFrame(DecodedFrame &frame) { - for (unsigned int i = 0; i < frames.frameList.size(); i++) - { - delete [] frames.frameList[i].rgbaImage.pixels; - } - - frames.clear(); + delete [] frame.rgbaImage.pixels; } void PNGDecoder::FillSupportedExtensions() diff --git a/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.h b/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.h index 3b62a23e93..c7dba764cb 100644 --- a/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.h +++ b/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.h @@ -28,7 +28,7 @@ class PNGDecoder : public IDecoder ~PNGDecoder() override = default; bool CanDecode(const std::string &filename) override; bool LoadFile(const std::string &filename, DecodedFrames &frames) override; - void FreeDecodedFrames(DecodedFrames &frames) override; + void FreeDecodedFrame(DecodedFrame &frame) override; const char* GetImageFormatName() override { return "PNG"; } const char* GetDecoderName() override { return "libpng"; } protected: |