1 #include <msp/core/refptr.h>
2 #include <msp/io/file.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/utils.h>
6 #include "imageloader.h"
8 #include "png/pngloader.h"
11 #include "jpeg/jpegloader.h"
14 #include "devil/devilloader.h"
17 #include "quartz/quartzloader.h"
25 ImageLoader::ImageLoader():
30 ImageLoader::~ImageLoader()
35 ImageLoader *ImageLoader::open_file(const string &fn)
39 RefPtr<IO::BufferedFile> file = new IO::BufferedFile(fn);
40 ImageLoader *loader = open_io(*file);
41 loader->source = file.release();
44 catch(const unsupported_image_format &e)
46 throw unsupported_image_format(format("%s: %s", fn, e.what()));
50 ImageLoader *ImageLoader::open_io(IO::Seekable &io)
52 Registry ®istry = get_registry();
55 registry.changed = false;
56 registry.loaders.sort(signature_size_compare);
59 if(registry.loaders.empty())
60 throw unsupported_image_format("no loaders");
62 string signature(registry.loaders.back()->get_signature_size(), 0);
63 unsigned sig_len = io.read(&signature[0], signature.size());
65 ImageLoader *loader = 0;
66 for(list<RegisterBase *>::const_iterator i=registry.loaders.begin(); (!loader && i!=registry.loaders.end()); ++i)
67 if((*i)->detect(signature))
68 loader = (*i)->create(io);
70 io.seek(0, IO::S_BEG);
75 for(unsigned i=0; i<sig_len; ++i)
76 append(sig_hex, " ", format("%02X", static_cast<unsigned char>(signature[i])));
77 throw unsupported_image_format(sig_hex);
83 void ImageLoader::load(Image::Data &data)
86 throw logic_error("already loaded");
88 if(state<HEADERS_LOADED)
91 data.pixels = data.owned_pixels = new char[data.stride*data.height];
96 void ImageLoader::load_headers(Image::Data &data)
98 if(state>=HEADERS_LOADED)
99 throw logic_error("headers already loaded");
102 state = HEADERS_LOADED;
105 ImageLoader::Registry &ImageLoader::get_registry()
107 static Registry registry;
108 static bool initialized = false;
112 register_loader<BmpLoader>();
114 register_loader<PngLoader>();
117 register_loader<JpegLoader>();
120 register_loader<DevilLoader>();
123 register_loader<QuartzLoader>();
129 bool ImageLoader::signature_size_compare(RegisterBase *r1, RegisterBase *r2)
131 return r1->get_signature_size()<r2->get_signature_size();
135 ImageLoader::Registry::Registry():
139 ImageLoader::Registry::~Registry()
141 for(list<RegisterBase *>::iterator i=loaders.begin(); i!=loaders.end(); ++i)
145 } // namespace Graphics