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