namespace Msp {
namespace Graphics {
-bool ImageLoader::registered_loaders_changed = false;
-
ImageLoader::ImageLoader():
source(0)
{ }
ImageLoader *ImageLoader::open_io(IO::Seekable &io)
{
- (void)RegisteredImageLoader<BmpLoader>::reg;
-#ifdef WITH_LIBPNG
- (void)RegisteredImageLoader<PngLoader>::reg;
-#endif
-#ifdef WITH_LIBJPEG
- (void)RegisteredImageLoader<JpegLoader>::reg;
-#endif
-#ifdef WITH_DEVIL
- (void)RegisteredImageLoader<DevilLoader>::reg;
-#endif
-#ifdef WITH_QUARTZ
- (void)RegisteredImageLoader<QuartzLoader>::reg;
-#endif
-
- list<RegisterBase *> &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<char> sig_buf(loaders.back()->get_signature_size());
+ vector<char> 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<RegisterBase *>::const_iterator i=loaders.begin(); (!loader && i!=loaders.end()); ++i)
+ for(list<RegisterBase *>::const_iterator i=registry.loaders.begin(); (!loader && i!=registry.loaders.end()); ++i)
if((*i)->detect(signature))
loader = (*i)->create(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::RegisterBase *> &ImageLoader::get_registered_loaders()
+ImageLoader::Registry &ImageLoader::get_registry()
{
- static list<RegisterBase *> regs;
- return regs;
+ static Registry registry;
+ static bool initialized = false;
+ if(!initialized)
+ {
+ initialized = true;
+ register_loader<BmpLoader>();
+#ifdef WITH_LIBPNG
+ register_loader<PngLoader>();
+#endif
+#ifdef WITH_LIBJPEG
+ register_loader<JpegLoader>();
+#endif
+#ifdef WITH_DEVIL
+ register_loader<DevilLoader>();
+#endif
+#ifdef WITH_QUARTZ
+ register_loader<QuartzLoader>();
+#endif
+ }
+ return registry;
}
bool ImageLoader::signature_size_compare(RegisterBase *r1, RegisterBase *r2)
return r1->get_signature_size()<r2->get_signature_size();
}
+
+ImageLoader::Registry::Registry():
+ changed(false)
+{ }
+
+ImageLoader::Registry::~Registry()
+{
+ for(list<RegisterBase *>::iterator i=loaders.begin(); i!=loaders.end(); ++i)
+ delete *i;
+}
+
} // namespace Graphics
} // namespace Msp
class RegisterBase
{
protected:
- RegisterBase();
+ RegisterBase() { }
public:
virtual ~RegisterBase() { }
virtual ImageLoader *create(IO::Seekable &) const = 0;
};
+ template<typename T>
+ 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<RegisterBase *> loaders;
+ bool changed;
+
+ Registry();
+ ~Registry();
+ };
+
private:
IO::Base *source;
- static std::list<RegisterBase *> &get_registered_loaders();
- static bool registered_loaders_changed;
- static bool signature_size_compare(RegisterBase *, RegisterBase *);
-
protected:
ImageLoader();
public:
static ImageLoader *open_file(const std::string &);
static ImageLoader *open_io(IO::Seekable &);
- virtual void load(Image::Data &) = 0;
-};
-
-
-template<typename T>
-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<typename T>
+ 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 T>
-typename RegisteredImageLoader<T>::Register RegisteredImageLoader<T>::reg;
+void ImageLoader::register_loader()
+{
+ Registry ®istry = get_registry();
+ registry.loaders.push_back(new RegisteredLoader<T>);
+ registry.changed = true;
+}
} // namespace Graphics
} // namespace Msp