]> git.tdb.fi Git - libs/gl.git/blob - source/ilwrap.cpp
be8442bee741a325199a4f45865a86bbbcea549c
[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 namespace Msp {
13 namespace GL {
14
15 Image::Image()
16 {
17         if(!init_done)
18         {
19                 ilInit();
20                 ilEnable(IL_ORIGIN_SET);
21                 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
22                 init_done=true;
23         }
24
25         ilGenImages(1, &id);
26 }
27
28 Image::~Image()
29 {
30         ilDeleteImages(1, &id);
31 }
32
33 void Image::load(const std::string &fn)
34 {
35         ilBindImage(id);
36         if(!ilLoadImage(const_cast<char *>(fn.c_str())))
37                 throw Exception("Error loading image "+fn);
38 }
39
40 PixelFormat Image::get_format() const
41 {
42         switch(ilGetInteger(IL_IMAGE_FORMAT))
43         {
44         case IL_COLOR_INDEX: return COLOR_INDEX;
45         case IL_LUMINANCE: return LUMINANCE;
46         case IL_LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
47         case IL_RGB: return RGB;
48         case IL_RGBA: return RGBA;
49         case IL_BGR: return BGR;
50         case IL_BGRA: return BGRA;
51         default: throw InvalidParameterValue("Unknown pixel format in image");
52         }
53 }
54
55 unsigned Image::get_width() const
56 {
57         return ilGetInteger(IL_IMAGE_WIDTH);
58 }
59
60 unsigned Image::get_height() const
61 {
62         return ilGetInteger(IL_IMAGE_HEIGHT);
63 }
64
65 const void *Image::get_data() const
66 {
67         return ilGetData();
68 }
69
70 bool Image::init_done=false;
71
72 } // namespace GL
73 } // namespace Msp