]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/image.cpp
Replace Image::load_memory with load_io
[libs/gui.git] / source / graphics / image.cpp
1 #ifdef WITH_DEVIL
2 #include <IL/il.h>
3 #endif
4 #include <msp/fs/utils.h>
5 #include <msp/io/file.h>
6 #include <msp/io/memory.h>
7 #include "image.h"
8 #include "image_devil.h"
9 #include "image_png.h"
10 #include "image_private.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace Graphics {
16
17 Image::Private::Private()
18 {
19 #ifdef WITH_DEVIL
20         id = 0;
21 #endif
22         fmt = RGB;
23         width = 0;
24         height = 0;
25         data = 0;
26 }
27
28
29 Image::Image():
30         priv(new Private)
31 {
32 #if !defined(WITH_DEVIL) && !defined(WITH_LIBPNG)
33         throw runtime_error("no image support");
34 #endif
35 }
36
37 Image::~Image()
38 {
39 #ifdef WITH_DEVIL
40         if(priv->id)
41                 ilDeleteImages(1, &priv->id);
42         else
43 #endif
44         delete[] priv->data;
45         delete priv;
46 }
47
48 void Image::load_file(const string &fn)
49 {
50         string ext = FS::extpart(fn);
51 #ifdef WITH_LIBPNG
52         if(ext==".png")
53         {
54                 IO::BufferedFile file(fn);
55                 load_png(file, *priv, 0, 0);
56         }
57         else
58 #endif
59         {
60 #ifdef WITH_DEVIL
61                 load_devil_file(fn, *priv);
62 #else
63                 throw unsupported_image_format("DevIL needed for non-PNG images");
64 #endif
65         }
66         (void)fn;
67 }
68
69 void Image::load_io(IO::Base &io)
70 {
71         char sig_buf[8];
72         unsigned sig_len = io.read(sig_buf, sizeof(sig_buf));
73 #ifdef WITH_LIBPNG
74         if(sig_len==sizeof(sig_buf) && is_png(sig_buf, sig_len))
75                 load_png(io, *priv, sig_buf, sig_len);
76         else
77 #endif
78         {
79 #ifdef WITH_DEVIL
80                 load_devil_io(io, *priv, sig_buf, sig_len);
81 #else
82                 throw unsupported_image_format("DevIL needed for non-PNG images");
83 #endif
84         }
85 }
86
87 PixelFormat Image::get_format() const
88 {
89         return priv->fmt;
90 }
91
92 unsigned Image::get_width() const
93 {
94         return priv->width;
95 }
96
97 unsigned Image::get_height() const
98 {
99         return priv->height;
100 }
101
102 const void *Image::get_data() const
103 {
104         return priv->data;
105 }
106
107 } // namespace Graphics
108 } // namespace Msp