]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/imageloader.cpp
Include the matching header first in .cpp files
[libs/gui.git] / source / graphics / imageloader.cpp
1 #include "imageloader.h"
2 #include <msp/core/refptr.h>
3 #include <msp/io/file.h>
4 #include <msp/strings/format.h>
5 #include <msp/strings/utils.h>
6 #include "bmploader.h"
7 #ifdef WITH_LIBPNG
8 #include "png/pngloader.h"
9 #endif
10 #ifdef WITH_LIBJPEG
11 #include "jpeg/jpegloader.h"
12 #endif
13 #ifdef WITH_DEVIL
14 #include "devil/devilloader.h"
15 #endif
16 #ifdef WITH_QUARTZ
17 #include "quartz/quartzloader.h"
18 #endif
19
20 using namespace std;
21
22 namespace Msp {
23 namespace Graphics {
24
25 ImageLoader::ImageLoader():
26         source(0),
27         state(INITIAL)
28 { }
29
30 ImageLoader::~ImageLoader()
31 {
32         delete source;
33 }
34
35 bool ImageLoader::detect_signature(const std::string &sig)
36 {
37         Registry &registry = get_registry();
38         for(const RegisterBase *r: registry.loaders)
39                 if(r->detect(sig))
40                         return true;
41         return false;
42 }
43
44 ImageLoader *ImageLoader::open_file(const string &fn)
45 {
46         try
47         {
48                 RefPtr<IO::BufferedFile> file = new IO::BufferedFile(fn);
49                 ImageLoader *loader = open_io(*file);
50                 loader->source = file.release();
51                 return loader;
52         }
53         catch(const unsupported_image_format &e)
54         {
55                 throw unsupported_image_format(format("%s: %s", fn, e.what()));
56         }
57 }
58
59 ImageLoader *ImageLoader::open_io(IO::Seekable &io)
60 {
61         Registry &registry = get_registry();
62         if(registry.changed)
63         {
64                 registry.changed = false;
65                 registry.loaders.sort(signature_size_compare);
66         }
67
68         if(registry.loaders.empty())
69                 throw unsupported_image_format("no loaders");
70
71         string signature(registry.loaders.back()->get_signature_size(), 0);
72         unsigned sig_len = io.read(&signature[0], signature.size());
73
74         ImageLoader *loader = 0;
75         for(auto i=registry.loaders.begin(); (!loader && i!=registry.loaders.end()); ++i)
76                 if((*i)->detect(signature))
77                         loader = (*i)->create(io);
78
79         io.seek(0, IO::S_BEG);
80
81         if(!loader)
82         {
83                 string sig_hex;
84                 for(unsigned i=0; i<sig_len; ++i)
85                         append(sig_hex, " ", format("%02X", static_cast<unsigned char>(signature[i])));
86                 throw unsupported_image_format(sig_hex);
87         }
88
89         return loader;
90 }
91
92 void ImageLoader::load(Image::Data &data)
93 {
94         if(state>=FINISHED)
95                 throw logic_error("already loaded");
96
97         if(state<HEADERS_LOADED)
98                 load_headers_(data);
99         if(!data.pixels)
100                 data.pixels = data.owned_pixels = new char[data.stride*data.height];
101         load_pixels_(data);
102         state = FINISHED;
103 }
104
105 void ImageLoader::load_headers(Image::Data &data)
106 {
107         if(state>=HEADERS_LOADED)
108                 throw logic_error("headers already loaded");
109
110         load_headers_(data);
111         state = HEADERS_LOADED;
112 }
113
114 ImageLoader::Registry &ImageLoader::get_registry()
115 {
116         static Registry registry;
117         static bool initialized = false;
118         if(!initialized)
119         {
120                 initialized = true;
121                 register_loader<BmpLoader>();
122 #ifdef WITH_LIBPNG
123                 register_loader<PngLoader>();
124 #endif
125 #ifdef WITH_LIBJPEG
126                 register_loader<JpegLoader>();
127 #endif
128 #ifdef WITH_DEVIL
129                 register_loader<DevilLoader>();
130 #endif
131 #ifdef WITH_QUARTZ
132                 register_loader<QuartzLoader>();
133 #endif
134         }
135         return registry;
136 }
137
138 bool ImageLoader::signature_size_compare(RegisterBase *r1, RegisterBase *r2)
139 {
140         return r1->get_signature_size()<r2->get_signature_size();
141 }
142
143
144 ImageLoader::Registry::Registry():
145         changed(false)
146 { }
147
148 ImageLoader::Registry::~Registry()
149 {
150         for(auto l: loaders)
151                 delete l;
152 }
153
154 } // namespace Graphics
155 } // namespace Msp