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