From d3bc6c9c2cfaaedaabfd4b5b7bf4e1da2de51331 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 31 Jan 2021 17:19:42 +0200 Subject: [PATCH] Register image loaders more explicitly The automatic registration just doesn't work reliably enough; there was already a hack to work around the problems, so might as well make that the official way. --- source/graphics/bmploader.h | 2 +- source/graphics/devil/devilloader.h | 2 +- source/graphics/imageloader.cpp | 73 +++++++++++++++------------ source/graphics/imageloader.h | 57 ++++++++++++--------- source/graphics/jpeg/jpegloader.h | 2 +- source/graphics/png/pngloader.h | 2 +- source/graphics/quartz/quartzloader.h | 2 +- 7 files changed, 79 insertions(+), 61 deletions(-) diff --git a/source/graphics/bmploader.h b/source/graphics/bmploader.h index db9af1d..273e104 100644 --- a/source/graphics/bmploader.h +++ b/source/graphics/bmploader.h @@ -6,7 +6,7 @@ namespace Msp { namespace Graphics { -class BmpLoader: public RegisteredImageLoader +class BmpLoader: public ImageLoader { private: IO::Base &io; diff --git a/source/graphics/devil/devilloader.h b/source/graphics/devil/devilloader.h index e94a31b..42ac2b2 100644 --- a/source/graphics/devil/devilloader.h +++ b/source/graphics/devil/devilloader.h @@ -6,7 +6,7 @@ namespace Msp { namespace Graphics { -class DevilLoader: public RegisteredImageLoader +class DevilLoader: public ImageLoader { private: IO::Base &io; diff --git a/source/graphics/imageloader.cpp b/source/graphics/imageloader.cpp index 965c7b3..da89798 100644 --- a/source/graphics/imageloader.cpp +++ b/source/graphics/imageloader.cpp @@ -21,8 +21,6 @@ using namespace std; namespace Msp { namespace Graphics { -bool ImageLoader::registered_loaders_changed = false; - ImageLoader::ImageLoader(): source(0) { } @@ -49,36 +47,22 @@ ImageLoader *ImageLoader::open_file(const string &fn) ImageLoader *ImageLoader::open_io(IO::Seekable &io) { - (void)RegisteredImageLoader::reg; -#ifdef WITH_LIBPNG - (void)RegisteredImageLoader::reg; -#endif -#ifdef WITH_LIBJPEG - (void)RegisteredImageLoader::reg; -#endif -#ifdef WITH_DEVIL - (void)RegisteredImageLoader::reg; -#endif -#ifdef WITH_QUARTZ - (void)RegisteredImageLoader::reg; -#endif - - list &loaders = get_registered_loaders(); - if(registered_loaders_changed) + Registry ®istry = get_registry(); + if(registry.changed) { - registered_loaders_changed = false; - loaders.sort(signature_size_compare); + registry.changed = false; + registry.loaders.sort(signature_size_compare); } - if(loaders.empty()) + if(registry.loaders.empty()) throw unsupported_image_format("no loaders"); - vector sig_buf(loaders.back()->get_signature_size()); + vector sig_buf(registry.loaders.back()->get_signature_size()); unsigned sig_len = io.read(&sig_buf[0], sig_buf.size()); string signature(sig_buf.begin(), sig_buf.end()); ImageLoader *loader = 0; - for(list::const_iterator i=loaders.begin(); (!loader && i!=loaders.end()); ++i) + for(list::const_iterator i=registry.loaders.begin(); (!loader && i!=registry.loaders.end()); ++i) if((*i)->detect(signature)) loader = (*i)->create(io); @@ -99,18 +83,34 @@ ImageLoader *ImageLoader::open_io(IO::Seekable &io) return loader; } - -ImageLoader::RegisterBase::RegisterBase() +void ImageLoader::load(Image::Data &data) { - get_registered_loaders().push_back(this); - registered_loaders_changed = true; + load_headers(data); + load_data(data); } - -list &ImageLoader::get_registered_loaders() +ImageLoader::Registry &ImageLoader::get_registry() { - static list regs; - return regs; + static Registry registry; + static bool initialized = false; + if(!initialized) + { + initialized = true; + register_loader(); +#ifdef WITH_LIBPNG + register_loader(); +#endif +#ifdef WITH_LIBJPEG + register_loader(); +#endif +#ifdef WITH_DEVIL + register_loader(); +#endif +#ifdef WITH_QUARTZ + register_loader(); +#endif + } + return registry; } bool ImageLoader::signature_size_compare(RegisterBase *r1, RegisterBase *r2) @@ -118,5 +118,16 @@ bool ImageLoader::signature_size_compare(RegisterBase *r1, RegisterBase *r2) return r1->get_signature_size()get_signature_size(); } + +ImageLoader::Registry::Registry(): + changed(false) +{ } + +ImageLoader::Registry::~Registry() +{ + for(list::iterator i=loaders.begin(); i!=loaders.end(); ++i) + delete *i; +} + } // namespace Graphics } // namespace Msp diff --git a/source/graphics/imageloader.h b/source/graphics/imageloader.h index 031ecb4..f0b8d52 100644 --- a/source/graphics/imageloader.h +++ b/source/graphics/imageloader.h @@ -27,7 +27,7 @@ protected: class RegisterBase { protected: - RegisterBase(); + RegisterBase() { } public: virtual ~RegisterBase() { } @@ -36,13 +36,27 @@ protected: virtual ImageLoader *create(IO::Seekable &) const = 0; }; + template + class RegisteredLoader: public RegisterBase + { + public: + virtual unsigned get_signature_size() const { return T::get_signature_size(); } + virtual bool detect(const std::string &s) const { return T::detect(s); } + virtual ImageLoader *create(IO::Seekable &io) const { return new T(io); } + }; + + struct Registry + { + std::list loaders; + bool changed; + + Registry(); + ~Registry(); + }; + private: IO::Base *source; - static std::list &get_registered_loaders(); - static bool registered_loaders_changed; - static bool signature_size_compare(RegisterBase *, RegisterBase *); - protected: ImageLoader(); public: @@ -51,32 +65,25 @@ public: static ImageLoader *open_file(const std::string &); static ImageLoader *open_io(IO::Seekable &); - virtual void load(Image::Data &) = 0; -}; - - -template -class RegisteredImageLoader: public ImageLoader -{ - friend class ImageLoader; + virtual void load(Image::Data &); + virtual void load_headers(Image::Data &) { } + virtual void load_data(Image::Data &) { } + template + static void register_loader(); private: - class Register: public RegisterBase - { - public: - virtual unsigned get_signature_size() const { return T::get_signature_size(); } - virtual bool detect(const std::string &s) const { return T::detect(s); } - virtual ImageLoader *create(IO::Seekable &io) const { return new T(io); } - }; - - static Register reg; + static Registry &get_registry(); -protected: - RegisteredImageLoader() { (void)reg; } + static bool signature_size_compare(RegisterBase *, RegisterBase *); }; template -typename RegisteredImageLoader::Register RegisteredImageLoader::reg; +void ImageLoader::register_loader() +{ + Registry ®istry = get_registry(); + registry.loaders.push_back(new RegisteredLoader); + registry.changed = true; +} } // namespace Graphics } // namespace Msp diff --git a/source/graphics/jpeg/jpegloader.h b/source/graphics/jpeg/jpegloader.h index b93d5bc..04e8917 100644 --- a/source/graphics/jpeg/jpegloader.h +++ b/source/graphics/jpeg/jpegloader.h @@ -6,7 +6,7 @@ namespace Msp { namespace Graphics { -class JpegLoader: public RegisteredImageLoader +class JpegLoader: public ImageLoader { private: struct Private; diff --git a/source/graphics/png/pngloader.h b/source/graphics/png/pngloader.h index de8b535..6ed57cc 100644 --- a/source/graphics/png/pngloader.h +++ b/source/graphics/png/pngloader.h @@ -6,7 +6,7 @@ namespace Msp { namespace Graphics { -class PngLoader: public RegisteredImageLoader +class PngLoader: public ImageLoader { private: struct Private; diff --git a/source/graphics/quartz/quartzloader.h b/source/graphics/quartz/quartzloader.h index 963e45c..4885ff5 100644 --- a/source/graphics/quartz/quartzloader.h +++ b/source/graphics/quartz/quartzloader.h @@ -6,7 +6,7 @@ namespace Msp { namespace Graphics { -class QuartzLoader: public RegisteredImageLoader +class QuartzLoader: public ImageLoader { private: struct Private; -- 2.45.2