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