diff options
author | Lukas Rusak <lorusak@gmail.com> | 2023-04-21 19:48:02 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-21 19:48:02 -0700 |
commit | 63cb92e1f2fc75f4d6d0fc951655ffda05918d01 (patch) | |
tree | 5eaaf01b1caf2a753ef2e04cd5c3e1f363e04480 /tools | |
parent | ce74a2a0f85e1983e526b59c543da9ac56d1fa64 (diff) | |
parent | 79c9268aa31ab1ca2f8bd89266dffa4b3556a52f (diff) |
Merge pull request #23150 from lrusak/TexturePacker-PR-1
TexturePacker: first pass at modernization
Diffstat (limited to 'tools')
11 files changed, 51 insertions, 126 deletions
diff --git a/tools/depends/native/TexturePacker/src/DecoderManager.cpp b/tools/depends/native/TexturePacker/src/DecoderManager.cpp index 8f6a9041bb..1b53b0ff11 100644 --- a/tools/depends/native/TexturePacker/src/DecoderManager.cpp +++ b/tools/depends/native/TexturePacker/src/DecoderManager.cpp @@ -20,45 +20,31 @@ #include <cstdio> #include "DecoderManager.h" -bool DecoderManager::verbose; -std::vector<IDecoder *> DecoderManager::m_decoders; - // ADD new decoders here // include decoders #include "PNGDecoder.h" #include "JPGDecoder.h" #include "GIFDecoder.h" -void DecoderManager::InstantiateDecoders() +DecoderManager::DecoderManager() { - m_decoders.push_back(new PNGDecoder()); - m_decoders.push_back(new JPGDecoder()); - m_decoders.push_back(new GIFDecoder()); -} - -void DecoderManager::FreeDecoders() -{ - for (unsigned int i = 0; i < m_decoders.size(); i++) - { - delete m_decoders[i]; - } - m_decoders.clear(); + m_decoders.emplace_back(std::make_unique<PNGDecoder>()); + m_decoders.emplace_back(std::make_unique<JPGDecoder>()); + m_decoders.emplace_back(std::make_unique<GIFDecoder>()); } // returns true for png, bmp, tga, jpg and dds files, otherwise returns false -bool DecoderManager::IsSupportedGraphicsFile(char *strFileName) +bool DecoderManager::IsSupportedGraphicsFile(std::string_view filename) { - std::string filename = strFileName; if (filename.length() < 4) return false; - for (unsigned int i = 0; i < m_decoders.size(); i++) + for (const auto& decoder : m_decoders) { - const std::vector<std::string> extensions = m_decoders[i]->GetSupportedExtensions(); - for (unsigned int n = 0; n < extensions.size(); n++) + const std::vector<std::string> extensions = decoder->GetSupportedExtensions(); + for (const auto& extension : extensions) { - int extLen = extensions[n].length(); - if (std::string::npos != filename.rfind(extensions[n].c_str(), filename.length() - extLen, extLen)) + if (std::string::npos != filename.rfind(extension, filename.length() - extension.length())) { return true; } @@ -69,20 +55,15 @@ bool DecoderManager::IsSupportedGraphicsFile(char *strFileName) bool DecoderManager::LoadFile(const std::string &filename, DecodedFrames &frames) { - for (unsigned int i = 0; i < m_decoders.size(); i++) + for (const auto& decoder : m_decoders) { - if (m_decoders[i]->CanDecode(filename)) + if (decoder->CanDecode(filename)) { if (verbose) - fprintf(stdout, "This is a %s - lets load it via %s...\n", - m_decoders[i]->GetImageFormatName(), m_decoders[i]->GetDecoderName()); - return m_decoders[i]->LoadFile(filename, frames); + fprintf(stdout, "This is a %s - lets load it via %s...\n", decoder->GetImageFormatName(), + decoder->GetDecoderName()); + return decoder->LoadFile(filename, frames); } } return false; } - -void DecoderManager::FreeDecodedFrames(DecodedFrames &frames) -{ - frames.clear(); -} diff --git a/tools/depends/native/TexturePacker/src/DecoderManager.h b/tools/depends/native/TexturePacker/src/DecoderManager.h index 2c59aa20a5..1337c9d7a3 100644 --- a/tools/depends/native/TexturePacker/src/DecoderManager.h +++ b/tools/depends/native/TexturePacker/src/DecoderManager.h @@ -22,16 +22,19 @@ #include "IDecoder.h" +#include <memory> +#include <string_view> + class DecoderManager { public: - static void InstantiateDecoders(); - static void FreeDecoders(); - static bool IsSupportedGraphicsFile(char *strFileName); - static bool LoadFile(const std::string &filename, DecodedFrames &frames); - static void FreeDecodedFrames(DecodedFrames &frames); - static bool verbose; + DecoderManager(); + ~DecoderManager() = default; + + bool IsSupportedGraphicsFile(std::string_view filename); + bool LoadFile(const std::string& filename, DecodedFrames& frames); + bool verbose; private: - static std::vector<IDecoder *> m_decoders; + std::vector<std::unique_ptr<IDecoder>> m_decoders; }; diff --git a/tools/depends/native/TexturePacker/src/Makefile.am b/tools/depends/native/TexturePacker/src/Makefile.am index d6b676f9f2..1b9e7ca1a9 100644 --- a/tools/depends/native/TexturePacker/src/Makefile.am +++ b/tools/depends/native/TexturePacker/src/Makefile.am @@ -2,7 +2,7 @@ AUTOMAKE_OPTIONS = subdir-objects AM_CFLAGS = -DTARGET_POSIX AM_CFLAGS += @EXTRA_DEFINES@ -AM_CXXFLAGS = $(AM_CFLAGS) -std=c++0x +AM_CXXFLAGS = $(AM_CFLAGS) -std=c++17 AM_CPPFLAGS = \ -I. \ diff --git a/tools/depends/native/TexturePacker/src/TexturePacker.cpp b/tools/depends/native/TexturePacker/src/TexturePacker.cpp index a6fd4284f1..e61d6b8eb0 100644 --- a/tools/depends/native/TexturePacker/src/TexturePacker.cpp +++ b/tools/depends/native/TexturePacker/src/TexturePacker.cpp @@ -51,6 +51,8 @@ #define DIR_SEPARATOR '/' +static DecoderManager decoderManager; + const char *GetFormatString(unsigned int format) { switch (format) @@ -103,7 +105,7 @@ void CreateSkeletonHeaderImpl(CXBTFWriter& xbtfWriter, CreateSkeletonHeaderImpl(xbtfWriter, fullPath + DIR_SEPARATOR + dp->d_name, tmpPath + dp->d_name); } - else if (DecoderManager::IsSupportedGraphicsFile(dp->d_name)) + else if (decoderManager.IsSupportedGraphicsFile(dp->d_name)) { std::string fileName = ""; if (relativePath.size() > 0) @@ -202,7 +204,7 @@ CXBTFFrame createXBTFFrame(RGBAImage &image, CXBTFWriter& writer, double maxMSE, int width, height; unsigned int format = 0; - unsigned char* argb = (unsigned char*)image.pixels; + unsigned char* argb = (unsigned char*)image.pixels.data(); width = image.width; height = image.height; @@ -287,7 +289,7 @@ int createBundle(const std::string& InputDir, const std::string& OutputFile, dou output += ' '; DecodedFrames frames; - bool loaded = DecoderManager::LoadFile(fullPath, frames); + bool loaded = decoderManager.LoadFile(fullPath, frames); if (!loaded) { @@ -300,9 +302,8 @@ int createBundle(const std::string& InputDir, const std::string& OutputFile, dou if (dupecheck) { for (unsigned int j = 0; j < frames.frameList.size(); j++) - MD5Update(&ctx, - (const uint8_t*)frames.frameList[j].rgbaImage.pixels, - frames.frameList[j].rgbaImage.height * frames.frameList[j].rgbaImage.pitch); + MD5Update(&ctx, (const uint8_t*)frames.frameList[j].rgbaImage.pixels.data(), + frames.frameList[j].rgbaImage.height * frames.frameList[j].rgbaImage.pitch); if (checkDupe(&ctx,hashes,dupes,i)) { @@ -326,7 +327,6 @@ int createBundle(const std::string& InputDir, const std::string& OutputFile, dou frame.GetWidth(), frame.GetHeight(), frame.GetUnpackedSize()); } } - DecoderManager::FreeDecodedFrames(frames); file.SetLoop(0); writer.UpdateFile(file); @@ -386,7 +386,7 @@ int main(int argc, char* argv[]) } else if (!strcmp(args[i], "-verbose")) { - DecoderManager::verbose = true; + decoderManager.verbose = true; } else if (!platform_stricmp(args[i], "-output") || !platform_stricmp(args[i], "-o")) { @@ -413,8 +413,6 @@ int main(int argc, char* argv[]) if (pos != InputDir.length() - 1) InputDir += DIR_SEPARATOR; - double maxMSE = 1.5; // HQ only please - DecoderManager::InstantiateDecoders(); + double maxMSE = 1.5; // HQ only please createBundle(InputDir, OutputFilename, maxMSE, flags, dupecheck); - DecoderManager::FreeDecoders(); } diff --git a/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.cpp b/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.cpp index 5bdfacd4eb..b98afed75f 100644 --- a/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.cpp +++ b/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.cpp @@ -50,14 +50,13 @@ bool GIFDecoder::LoadFile(const std::string &filename, DecodedFrames &frames) { DecodedFrame frame; - frame.rgbaImage.pixels = (char *)new char[frameSize]; - memcpy(frame.rgbaImage.pixels, extractedFrames[i]->m_pImage, frameSize); + frame.rgbaImage.pixels.resize(frameSize); + memcpy(frame.rgbaImage.pixels.data(), extractedFrames[i]->m_pImage, frameSize); frame.rgbaImage.height = height; frame.rgbaImage.width = width; frame.rgbaImage.bbp = 32; frame.rgbaImage.pitch = pitch; frame.delay = extractedFrames[i]->m_delay; - frame.decoder = this; frames.frameList.push_back(frame); } @@ -68,11 +67,6 @@ bool GIFDecoder::LoadFile(const std::string &filename, DecodedFrames &frames) return result; } -void GIFDecoder::FreeDecodedFrame(DecodedFrame &frame) -{ - delete [] frame.rgbaImage.pixels; -} - void GIFDecoder::FillSupportedExtensions() { m_supportedExtensions.emplace_back(".gif"); diff --git a/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.h b/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.h index 8f0b42d822..e63a5666f8 100644 --- a/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.h +++ b/tools/depends/native/TexturePacker/src/decoder/GIFDecoder.h @@ -27,8 +27,7 @@ class GIFDecoder : public IDecoder public: ~GIFDecoder() override = default; bool CanDecode(const std::string &filename) override; - bool LoadFile(const std::string &filename, DecodedFrames &frames) override; - void FreeDecodedFrame(DecodedFrame &frame) override; + bool LoadFile(const std::string& filename, DecodedFrames& frames) 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 c9ac346514..0896cbfac7 100644 --- a/tools/depends/native/TexturePacker/src/decoder/IDecoder.h +++ b/tools/depends/native/TexturePacker/src/decoder/IDecoder.h @@ -33,8 +33,7 @@ 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 bool LoadFile(const std::string& filename, DecodedFrames& frames) = 0; virtual const char* GetImageFormatName() = 0; virtual const char* GetDecoderName() = 0; @@ -56,7 +55,7 @@ class RGBAImage public: RGBAImage() = default; - char* pixels = nullptr; // image data + std::vector<uint8_t> pixels; int width = 0; // width int height = 0; // height int bbp = 0; // bits per pixel @@ -69,7 +68,6 @@ public: DecodedFrame() = default; RGBAImage rgbaImage; /* rgbaimage for this frame */ int delay = 0; /* Frame delay in ms */ - IDecoder* decoder = nullptr; /* Pointer to decoder */ }; class DecodedFrames @@ -77,22 +75,4 @@ class DecodedFrames public: DecodedFrames() = default; std::vector<DecodedFrame> frameList; - - 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(); - } }; diff --git a/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.cpp b/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.cpp index ce06d28882..fb51dd3d8c 100644 --- a/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.cpp +++ b/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.cpp @@ -88,10 +88,10 @@ bool JPGDecoder::LoadFile(const std::string &filename, DecodedFrames &frames) DecodedFrame frame; - frame.rgbaImage.pixels = (char *)new char[ImageSize]; + frame.rgbaImage.pixels.resize(ImageSize); unsigned char *scanlinebuff = new unsigned char[3 * cinfo.image_width]; - unsigned char *dst = (unsigned char *)frame.rgbaImage.pixels; + unsigned char* dst = (unsigned char*)frame.rgbaImage.pixels.data(); while (cinfo.output_scanline < cinfo.output_height) { jpeg_read_scanlines(&cinfo,&scanlinebuff,1); @@ -117,19 +117,12 @@ bool JPGDecoder::LoadFile(const std::string &filename, DecodedFrames &frames) 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::FreeDecodedFrame(DecodedFrame &frame) -{ - delete [] frame.rgbaImage.pixels; -} - void JPGDecoder::FillSupportedExtensions() { m_supportedExtensions.emplace_back(".jpg"); diff --git a/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.h b/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.h index bbf23ba7b3..68afb86039 100644 --- a/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.h +++ b/tools/depends/native/TexturePacker/src/decoder/JPGDecoder.h @@ -27,8 +27,7 @@ class JPGDecoder : public IDecoder public: ~JPGDecoder() override = default; bool CanDecode(const std::string &filename) override; - bool LoadFile(const std::string &filename, DecodedFrames &frames) override; - void FreeDecodedFrame(DecodedFrame &frame) override; + bool LoadFile(const std::string& filename, DecodedFrames& frames) 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 f327400f6b..92b8b10485 100644 --- a/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.cpp +++ b/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.cpp @@ -187,55 +187,34 @@ bool PNGDecoder::LoadFile(const std::string &filename, DecodedFrames &frames) // glTexImage2d requires rows to be 4-byte aligned // rowbytes += 3 - ((rowbytes-1) % 4); + DecodedFrame frame; + // Allocate the image_data as a big block, to be given to opengl - png_byte * image_data; - image_data = (png_byte*)new png_byte[rowbytes * temp_height * sizeof(png_byte)+15]; - if (image_data == NULL) - { - fprintf(stderr, "error: could not allocate memory for PNG image data\n"); - png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); - return false; - } + frame.rgbaImage.pixels.resize(rowbytes * temp_height * sizeof(png_byte) + 15); // row_pointers is for pointing to image_data for reading the png with libpng - png_bytep * row_pointers = (png_bytep*) new png_bytep[temp_height * sizeof(png_bytep)]; - if (row_pointers == NULL) - { - fprintf(stderr, "error: could not allocate memory for PNG row pointers\n"); - png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); - delete [] image_data; - return false; - } + std::vector<png_bytep> row_pointers; + row_pointers.resize(temp_height); // set the individual row_pointers to point at the correct offsets of image_data for (unsigned int i = 0; i < temp_height; i++) { - row_pointers[i] = image_data + i * rowbytes; + row_pointers[i] = frame.rgbaImage.pixels.data() + i * rowbytes; } // read the png into image_data through row_pointers - png_read_image(png_ptr, row_pointers); - - DecodedFrame frame; + png_read_image(png_ptr, row_pointers.data()); - frame.rgbaImage.pixels = (char *)image_data; frame.rgbaImage.height = temp_height; 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); - delete [] row_pointers; - return true; -} -void PNGDecoder::FreeDecodedFrame(DecodedFrame &frame) -{ - delete [] frame.rgbaImage.pixels; + return true; } void PNGDecoder::FillSupportedExtensions() diff --git a/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.h b/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.h index c7dba764cb..80207d1e32 100644 --- a/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.h +++ b/tools/depends/native/TexturePacker/src/decoder/PNGDecoder.h @@ -27,8 +27,7 @@ class PNGDecoder : public IDecoder public: ~PNGDecoder() override = default; bool CanDecode(const std::string &filename) override; - bool LoadFile(const std::string &filename, DecodedFrames &frames) override; - void FreeDecodedFrame(DecodedFrame &frame) override; + bool LoadFile(const std::string& filename, DecodedFrames& frames) override; const char* GetImageFormatName() override { return "PNG"; } const char* GetDecoderName() override { return "libpng"; } protected: |