]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/imageloader.h
Avoid using std::list
[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::vector<RegisterBase *> loaders;
57                 bool changed = false;
58
59                 ~Registry();
60         };
61
62 private:
63         IO::Base *source = nullptr;
64         State state = INITIAL;
65
66 protected:
67         ImageLoader() = default;
68 public:
69         virtual ~ImageLoader();
70
71         static bool detect_signature(const std::string &);
72         static ImageLoader *open_file(const std::string &);
73         static ImageLoader *open_io(IO::Seekable &);
74
75         virtual void load(Image::Data &);
76         virtual void load_headers(Image::Data &);
77 protected:
78         virtual void load_headers_(Image::Data &) = 0;
79         virtual void load_pixels_(Image::Data &) = 0;
80
81 public:
82         State get_state() const { return state; }
83
84         template<typename T>
85         static void register_loader();
86 private:
87         static Registry &get_registry();
88
89         static bool signature_size_compare(RegisterBase *, RegisterBase *);
90 };
91
92 template<typename T>
93 void ImageLoader::register_loader()
94 {
95         Registry &registry = get_registry();
96         registry.loaders.push_back(new RegisteredLoader<T>);
97         registry.changed = true;
98 }
99
100 } // namespace Graphics
101 } // namespace Msp
102
103 #endif