]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/imageloader.h
9645f116ffd4228a1098c425339fe7a0f39f3ec3
[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 };
14
15 class bad_image_data: public std::runtime_error
16 {
17 public:
18         bad_image_data(const std::string &w): std::runtime_error(w) { }
19 };
20
21
22 class ImageLoader
23 {
24 public:
25         enum State
26         {
27                 INITIAL,
28                 HEADERS_LOADED,
29                 FINISHED
30         };
31
32 protected:
33         class RegisterBase
34         {
35         protected:
36                 RegisterBase() { }
37         public:
38                 virtual ~RegisterBase() { }
39
40                 virtual unsigned get_signature_size() const = 0;
41                 virtual bool detect(const std::string &) const = 0;
42                 virtual ImageLoader *create(IO::Seekable &) const = 0;
43         };
44
45         template<typename T>
46         class RegisteredLoader: public RegisterBase
47         {
48         public:
49                 unsigned get_signature_size() const override { return T::get_signature_size(); }
50                 bool detect(const std::string &s) const override { return T::detect(s); }
51                 ImageLoader *create(IO::Seekable &io) const override { return new T(io); }
52         };
53
54         struct Registry
55         {
56                 std::list<RegisterBase *> loaders;
57                 bool changed;
58
59                 Registry();
60                 ~Registry();
61         };
62
63 private:
64         IO::Base *source;
65         State state;
66
67 protected:
68         ImageLoader();
69 public:
70         virtual ~ImageLoader();
71
72         static bool detect_signature(const std::string &);
73         static ImageLoader *open_file(const std::string &);
74         static ImageLoader *open_io(IO::Seekable &);
75
76         virtual void load(Image::Data &);
77         virtual void load_headers(Image::Data &);
78 protected:
79         virtual void load_headers_(Image::Data &) = 0;
80         virtual void load_pixels_(Image::Data &) = 0;
81
82 public:
83         State get_state() const { return state; }
84
85         template<typename T>
86         static void register_loader();
87 private:
88         static Registry &get_registry();
89
90         static bool signature_size_compare(RegisterBase *, RegisterBase *);
91 };
92
93 template<typename T>
94 void ImageLoader::register_loader()
95 {
96         Registry &registry = get_registry();
97         registry.loaders.push_back(new RegisteredLoader<T>);
98         registry.changed = true;
99 }
100
101 } // namespace Graphics
102 } // namespace Msp
103
104 #endif