]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/imageloader.h
Register image loaders more explicitly
[libs/gui.git] / source / graphics / imageloader.h
1 #ifndef MSP_GRAPHICS_IMAGELOADER_H_
2 #define MSP_GRAPHICS_IMAGELOADER_H_
3
4 #include "image.h"
5
6 namespace Msp {
7 namespace Graphics {
8
9 class unsupported_image_format: public std::runtime_error
10 {
11 public:
12         unsupported_image_format(const std::string &w): std::runtime_error(w) { }
13         virtual ~unsupported_image_format() throw() { }
14 };
15
16 class bad_image_data: public std::runtime_error
17 {
18 public:
19         bad_image_data(const std::string &w): std::runtime_error(w) { }
20         virtual ~bad_image_data() throw() { }
21 };
22
23
24 class ImageLoader
25 {
26 protected:
27         class RegisterBase
28         {
29         protected:
30                 RegisterBase() { }
31         public:
32                 virtual ~RegisterBase() { }
33
34                 virtual unsigned get_signature_size() const = 0;
35                 virtual bool detect(const std::string &) const = 0;
36                 virtual ImageLoader *create(IO::Seekable &) const = 0;
37         };
38
39         template<typename T>
40         class RegisteredLoader: public RegisterBase
41         {
42         public:
43                 virtual unsigned get_signature_size() const { return T::get_signature_size(); }
44                 virtual bool detect(const std::string &s) const { return T::detect(s); }
45                 virtual ImageLoader *create(IO::Seekable &io) const { return new T(io); }
46         };
47
48         struct Registry
49         {
50                 std::list<RegisterBase *> loaders;
51                 bool changed;
52
53                 Registry();
54                 ~Registry();
55         };
56
57 private:
58         IO::Base *source;
59
60 protected:
61         ImageLoader();
62 public:
63         virtual ~ImageLoader();
64
65         static ImageLoader *open_file(const std::string &);
66         static ImageLoader *open_io(IO::Seekable &);
67
68         virtual void load(Image::Data &);
69         virtual void load_headers(Image::Data &) { }
70         virtual void load_data(Image::Data &) { }
71
72         template<typename T>
73         static void register_loader();
74 private:
75         static Registry &get_registry();
76
77         static bool signature_size_compare(RegisterBase *, RegisterBase *);
78 };
79
80 template<typename T>
81 void ImageLoader::register_loader()
82 {
83         Registry &registry = get_registry();
84         registry.loaders.push_back(new RegisteredLoader<T>);
85         registry.changed = true;
86 }
87
88 } // namespace Graphics
89 } // namespace Msp
90
91 #endif