]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/imageloader.cpp
Add a helper class for registering image loaders
[libs/gui.git] / source / graphics / imageloader.cpp
1 #include <msp/core/refptr.h>
2 #include <msp/io/file.h>
3 #include <msp/strings/format.h>
4 #include "imageloader.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace Graphics {
10
11 bool ImageLoader::registered_loaders_changed = false;
12
13 ImageLoader::ImageLoader():
14         source(0)
15 { }
16
17 ImageLoader::~ImageLoader()
18 {
19         delete source;
20 }
21
22 ImageLoader *ImageLoader::open_file(const string &fn)
23 {
24         try
25         {
26                 RefPtr<IO::BufferedFile> file = new IO::BufferedFile(fn);
27                 ImageLoader *loader = open_io(*file);
28                 loader->source = file.release();
29                 return loader;
30         }
31         catch(const unsupported_image_format &e)
32         {
33                 throw unsupported_image_format(format("%s: %s", fn, e.what()));
34         }
35 }
36
37 ImageLoader *ImageLoader::open_io(IO::Seekable &io)
38 {
39         list<RegisterBase *> &loaders = get_registered_loaders();
40         if(registered_loaders_changed)
41         {
42                 registered_loaders_changed = false;
43                 loaders.sort(signature_size_compare);
44         }
45
46         if(loaders.empty())
47                 throw unsupported_image_format("no loaders");
48
49         vector<char> sig_buf(loaders.back()->get_signature_size());
50         unsigned sig_len = io.read(&sig_buf[0], sig_buf.size());
51         string signature(sig_buf.begin(), sig_buf.end());
52
53         ImageLoader *loader = 0;
54         for(list<RegisterBase *>::const_iterator i=loaders.begin(); (!loader && i!=loaders.end()); ++i)
55                 if((*i)->detect(signature))
56                         loader = (*i)->create(io);
57
58         io.seek(0, IO::S_BEG);
59
60         if(!loader)
61         {
62                 string sig_hex;
63                 for(unsigned i=0; i<sig_len; ++i)
64                 {
65                         if(i)
66                                 sig_hex += ' ';
67                         sig_hex += format("%02X", static_cast<unsigned char>(sig_buf[i]));
68                 }
69                 throw unsupported_image_format(sig_hex);
70         }
71
72         return loader;
73 }
74
75
76 ImageLoader::RegisterBase::RegisterBase()
77 {
78         get_registered_loaders().push_back(this);
79         registered_loaders_changed = true;
80 }
81
82
83 list<ImageLoader::RegisterBase *> &ImageLoader::get_registered_loaders()
84 {
85         static list<RegisterBase *> regs;
86         return regs;
87 }
88
89 bool ImageLoader::signature_size_compare(RegisterBase *r1, RegisterBase *r2)
90 {
91         return r1->get_signature_size()<r2->get_signature_size();
92 }
93
94 } // namespace Graphics
95 } // namespace Msp