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