aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorphunkyfish <phunkyfish@gmail.com>2020-10-05 14:32:08 +0100
committerGitHub <noreply@github.com>2020-10-05 14:32:08 +0100
commit7bc88ff3cbf18fe3fdf5bf5359466ceb82ec1370 (patch)
tree64cacb2b5ef91ce100433ebe79d9a242f8e5430f
parent784f0b0e539c5d2eaa495402d26813bbcf8959de (diff)
parent3874e487e512c055f535ef27b100e76b55bdf453 (diff)
Merge pull request #18362 from basilgello/simplify-texturepacker
Simplify TexturePacker
-rw-r--r--tools/depends/native/TexturePacker/src/decoder/GIFDecoder.cpp27
-rw-r--r--tools/depends/native/TexturePacker/src/decoder/GIFDecoder.h4
-rw-r--r--tools/depends/native/TexturePacker/src/decoder/IDecoder.h70
-rw-r--r--tools/depends/native/TexturePacker/src/decoder/JPGDecoder.cpp13
-rw-r--r--tools/depends/native/TexturePacker/src/decoder/JPGDecoder.h2
-rw-r--r--tools/depends/native/TexturePacker/src/decoder/PNGDecoder.cpp13
-rw-r--r--tools/depends/native/TexturePacker/src/decoder/PNGDecoder.h2
7 files changed, 60 insertions, 71 deletions
diff --git a/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.cpp b/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.cpp
index 3ddb6a575a..5bdfacd4eb 100644
--- a/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.cpp
+++ b/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.cpp
@@ -33,6 +33,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()))
{
@@ -55,33 +57,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;
- frames.destroyFN = &gifDestroyFN;
- return true;
- }
- else
- {
- delete gifImage;
- return false;
+ result = true;
}
+ delete gifImage;
+ return result;
}
-void GIFDecoder::FreeDecodedFrames(DecodedFrames &frames)
-{
- for (unsigned int i = 0; i < frames.frameList.size(); i++)
- {
- delete [] frames.frameList[i].rgbaImage.pixels;
- }
- delete (GifHelper *)frames.user;
- frames.clear();
-}
-void GIFDecoder::gifDestroyFN(void* user)
+void GIFDecoder::FreeDecodedFrame(DecodedFrame &frame)
{
- delete (GifHelper *)user;
+ 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 c311ddaa82..8f0b42d822 100644
--- a/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.h
+++ b/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.h
@@ -28,11 +28,9 @@ 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:
void FillSupportedExtensions() override;
- private:
- static void gifDestroyFN(void* user);
};
diff --git a/tools/depends/native/TexturePacker/src/decoder/IDecoder.h b/tools/depends/native/TexturePacker/src/decoder/IDecoder.h
index 6e68aba1f1..c9ac346514 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:
@@ -41,6 +69,7 @@ public:
DecodedFrame() = default;
RGBAImage rgbaImage; /* rgbaimage for this frame */
int delay = 0; /* Frame delay in ms */
+ IDecoder* decoder = nullptr; /* Pointer to decoder */
};
class DecodedFrames
@@ -48,43 +77,22 @@ class DecodedFrames
public:
DecodedFrames() = default;
std::vector<DecodedFrame> frameList;
- void* user = nullptr; /* used internally*/
- void (*destroyFN)(void*) = nullptr;
void clear()
{
for (auto f : frameList)
{
- delete[] f.rgbaImage.pixels;
- }
- if (destroyFN)
- {
- destroyFN(user);
+ 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 c2d96a8fa9..ce06d28882 100644
--- a/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.cpp
+++ b/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.cpp
@@ -86,7 +86,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];
@@ -117,20 +116,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 b12e55f426..f327400f6b 100644
--- a/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.cpp
+++ b/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.cpp
@@ -216,7 +216,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;
@@ -224,6 +223,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);
@@ -231,14 +233,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: