]> git.tdb.fi Git - libs/gl.git/blob - source/ilwrap.cpp
a1ae1631776539ec5d2be163af3e8f86ced48549
[libs/gl.git] / source / ilwrap.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <IL/il.h>
9 #include <msp/core/except.h>
10 #include "ilwrap.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 Image::Image()
18 {
19         static bool init_done=false;
20
21         if(!init_done)
22         {
23                 ilInit();
24                 ilEnable(IL_ORIGIN_SET);
25                 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
26                 init_done=true;
27         }
28
29         ilGenImages(1, &id);
30 }
31
32 Image::~Image()
33 {
34         ilDeleteImages(1, &id);
35 }
36
37 void Image::load_file(const string &fn)
38 {
39         ilBindImage(id);
40         if(!ilLoadImage(const_cast<char *>(fn.c_str())))
41                 throw Exception("Error loading image "+fn);
42 }
43
44 void Image::load_lump(const void *data, unsigned size)
45 {
46         ilBindImage(id);
47         if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
48                 throw Exception("Error loading image from lump");
49 }
50
51 PixelFormat Image::get_format() const
52 {
53         switch(ilGetInteger(IL_IMAGE_FORMAT))
54         {
55         case IL_COLOR_INDEX: return COLOR_INDEX;
56         case IL_LUMINANCE: return LUMINANCE;
57         case IL_LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
58         case IL_RGB: return RGB;
59         case IL_RGBA: return RGBA;
60         case IL_BGR: return BGR;
61         case IL_BGRA: return BGRA;
62         default: throw InvalidParameterValue("Unknown pixel format in image");
63         }
64 }
65
66 unsigned Image::get_width() const
67 {
68         return ilGetInteger(IL_IMAGE_WIDTH);
69 }
70
71 unsigned Image::get_height() const
72 {
73         return ilGetInteger(IL_IMAGE_HEIGHT);
74 }
75
76 const void *Image::get_data() const
77 {
78         return ilGetData();
79 }
80
81 } // namespace GL
82 } // namespace Msp