]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/imageloader.cpp
Use the append function to build hex dump
[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         vector<char> sig_buf(registry.loaders.back()->get_signature_size());
63         unsigned sig_len = io.read(&sig_buf[0], sig_buf.size());
64         string signature(sig_buf.begin(), sig_buf.end());
65
66         ImageLoader *loader = 0;
67         for(list<RegisterBase *>::const_iterator i=registry.loaders.begin(); (!loader && i!=registry.loaders.end()); ++i)
68                 if((*i)->detect(signature))
69                         loader = (*i)->create(io);
70
71         io.seek(0, IO::S_BEG);
72
73         if(!loader)
74         {
75                 string sig_hex;
76                 for(unsigned i=0; i<sig_len; ++i)
77                         append(sig_hex, " ", format("%02X", static_cast<unsigned char>(sig_buf[i])));
78                 throw unsupported_image_format(sig_hex);
79         }
80
81         return loader;
82 }
83
84 void ImageLoader::load(Image::Data &data)
85 {
86         if(state>=FINISHED)
87                 throw logic_error("already loaded");
88
89         if(state<HEADERS_LOADED)
90                 load_headers_(data);
91         if(!data.pixels)
92                 data.pixels = data.owned_pixels = new char[data.stride*data.height];
93         load_pixels_(data);
94         state = FINISHED;
95 }
96
97 void ImageLoader::load_headers(Image::Data &data)
98 {
99         if(state>=HEADERS_LOADED)
100                 throw logic_error("headers already loaded");
101
102         load_headers_(data);
103         state = HEADERS_LOADED;
104 }
105
106 ImageLoader::Registry &ImageLoader::get_registry()
107 {
108         static Registry registry;
109         static bool initialized = false;
110         if(!initialized)
111         {
112                 initialized = true;
113                 register_loader<BmpLoader>();
114 #ifdef WITH_LIBPNG
115                 register_loader<PngLoader>();
116 #endif
117 #ifdef WITH_LIBJPEG
118                 register_loader<JpegLoader>();
119 #endif
120 #ifdef WITH_DEVIL
121                 register_loader<DevilLoader>();
122 #endif
123 #ifdef WITH_QUARTZ
124                 register_loader<QuartzLoader>();
125 #endif
126         }
127         return registry;
128 }
129
130 bool ImageLoader::signature_size_compare(RegisterBase *r1, RegisterBase *r2)
131 {
132         return r1->get_signature_size()<r2->get_signature_size();
133 }
134
135
136 ImageLoader::Registry::Registry():
137         changed(false)
138 { }
139
140 ImageLoader::Registry::~Registry()
141 {
142         for(list<RegisterBase *>::iterator i=loaders.begin(); i!=loaders.end(); ++i)
143                 delete *i;
144 }
145
146 } // namespace Graphics
147 } // namespace Msp