1 #include <msp/core/refptr.h>
2 #include <msp/io/file.h>
3 #include <msp/strings/format.h>
4 #include "imageloader.h"
6 #include "png/pngloader.h"
9 #include "jpeg/jpegloader.h"
12 #include "devil/devilloader.h"
15 #include "quartz/quartzloader.h"
23 bool ImageLoader::registered_loaders_changed = false;
25 ImageLoader::ImageLoader():
29 ImageLoader::~ImageLoader()
34 ImageLoader *ImageLoader::open_file(const string &fn)
38 RefPtr<IO::BufferedFile> file = new IO::BufferedFile(fn);
39 ImageLoader *loader = open_io(*file);
40 loader->source = file.release();
43 catch(const unsupported_image_format &e)
45 throw unsupported_image_format(format("%s: %s", fn, e.what()));
49 ImageLoader *ImageLoader::open_io(IO::Seekable &io)
52 (void)RegisteredImageLoader<PngLoader>::reg;
55 (void)RegisteredImageLoader<JpegLoader>::reg;
58 (void)RegisteredImageLoader<DevilLoader>::reg;
61 (void)RegisteredImageLoader<QuartzLoader>::reg;
64 list<RegisterBase *> &loaders = get_registered_loaders();
65 if(registered_loaders_changed)
67 registered_loaders_changed = false;
68 loaders.sort(signature_size_compare);
72 throw unsupported_image_format("no loaders");
74 vector<char> sig_buf(loaders.back()->get_signature_size());
75 unsigned sig_len = io.read(&sig_buf[0], sig_buf.size());
76 string signature(sig_buf.begin(), sig_buf.end());
78 ImageLoader *loader = 0;
79 for(list<RegisterBase *>::const_iterator i=loaders.begin(); (!loader && i!=loaders.end()); ++i)
80 if((*i)->detect(signature))
81 loader = (*i)->create(io);
83 io.seek(0, IO::S_BEG);
88 for(unsigned i=0; i<sig_len; ++i)
92 sig_hex += format("%02X", static_cast<unsigned char>(sig_buf[i]));
94 throw unsupported_image_format(sig_hex);
101 ImageLoader::RegisterBase::RegisterBase()
103 get_registered_loaders().push_back(this);
104 registered_loaders_changed = true;
108 list<ImageLoader::RegisterBase *> &ImageLoader::get_registered_loaders()
110 static list<RegisterBase *> regs;
114 bool ImageLoader::signature_size_compare(RegisterBase *r1, RegisterBase *r2)
116 return r1->get_signature_size()<r2->get_signature_size();
119 } // namespace Graphics