]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/imageloader.h
Add decorations for things which should be exported from the library
[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 #include "mspgui_api.h"
6
7 namespace Msp {
8 namespace Graphics {
9
10 class MSPGUI_API unsupported_image_format: public std::runtime_error
11 {
12 public:
13         unsupported_image_format(const std::string &w): std::runtime_error(w) { }
14 };
15
16 class MSPGUI_API bad_image_data: public std::runtime_error
17 {
18 public:
19         bad_image_data(const std::string &w): std::runtime_error(w) { }
20 };
21
22
23 class MSPGUI_API ImageLoader
24 {
25 public:
26         enum State
27         {
28                 INITIAL,
29                 HEADERS_LOADED,
30                 FINISHED
31         };
32
33 protected:
34         class RegisterBase
35         {
36         protected:
37                 RegisterBase() { }
38         public:
39                 virtual ~RegisterBase() { }
40
41                 virtual unsigned get_signature_size() const = 0;
42                 virtual bool detect(const std::string &) const = 0;
43                 virtual ImageLoader *create(IO::Seekable &) const = 0;
44         };
45
46         template<typename T>
47         class RegisteredLoader: public RegisterBase
48         {
49         public:
50                 unsigned get_signature_size() const override { return T::get_signature_size(); }
51                 bool detect(const std::string &s) const override { return T::detect(s); }
52                 ImageLoader *create(IO::Seekable &io) const override { return new T(io); }
53         };
54
55         struct Registry
56         {
57                 std::vector<RegisterBase *> loaders;
58                 bool changed = false;
59
60                 ~Registry();
61         };
62
63 private:
64         IO::Base *source = nullptr;
65         State state = INITIAL;
66
67 protected:
68         ImageLoader() = default;
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
91 template<typename T>
92 void ImageLoader::register_loader()
93 {
94         Registry &registry = get_registry();
95         registry.loaders.push_back(new RegisteredLoader<T>);
96         registry.changed = true;
97 }
98
99 } // namespace Graphics
100 } // namespace Msp
101
102 #endif