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