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