]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/imageloader.h
Avoid using too much CPU time in the event tester
[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 private:
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 protected:
40         template<typename T>
41         class Register: RegisterBase
42         {
43         public:
44                 virtual unsigned get_signature_size() const { return T::get_signature_size(); }
45                 virtual bool detect(const std::string &s) const { return T::detect(s); }
46                 virtual T *create(IO::Seekable &io) const { return new T(io); }
47         };
48
49 private:
50         IO::Base *source;
51
52         static std::list<RegisterBase *> &get_registered_loaders();
53         static bool registered_loaders_changed;
54         static bool signature_size_compare(RegisterBase *, RegisterBase *);
55
56 protected:
57         ImageLoader();
58 public:
59         virtual ~ImageLoader();
60
61         static ImageLoader *open_file(const std::string &);
62         static ImageLoader *open_io(IO::Seekable &);
63
64         virtual void load(Image::Data &) = 0;
65 };
66
67 } // namespace Graphics
68 } // namespace Msp
69
70 #endif