]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.cpp
Add vertex arrays and buffers
[libs/gl.git] / source / texture2d.cpp
1 #include <png.h>
2 #include <msp/core/error.h>
3 #include "texture2d.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 Texture2D::Texture2D()
11 {
12         target=GL_TEXTURE_2D;
13 }
14
15 /**
16 Uploads an image into the texture.  Direct wrapper for glTexImage2D.
17 */
18 void Texture2D::image(int level, int ifmt, sizei wd, sizei ht, int border, GLenum fmt, GLenum type, void *data)
19 {
20         maybe_bind();
21
22         glTexImage2D(target, level, ifmt, wd, ht, border, fmt, type, data);
23         width_=wd;
24         height_=ht;
25 }
26
27 /**
28 Uploads an image into the texture, with a simpler interface.
29 */
30 void Texture2D::image(int level, sizei wd, sizei ht, TextureFormat tfmt, void *data)
31 {
32         int ifmt;
33         int fmt;
34         int type;
35
36         switch(tfmt)
37         {
38         case LUMINANCE8:        ifmt=GL_LUMINANCE;       fmt=GL_LUMINANCE;       type=GL_UNSIGNED_BYTE; break;
39         case LUMINANCE8_ALPHA8: ifmt=GL_LUMINANCE_ALPHA; fmt=GL_LUMINANCE_ALPHA; type=GL_UNSIGNED_BYTE; break;
40         case RGB8:  ifmt=GL_RGB;  fmt=GL_RGB;  type=GL_UNSIGNED_BYTE;        break;
41         case RGBA8: ifmt=GL_RGBA; fmt=GL_RGBA; type=GL_UNSIGNED_INT_8_8_8_8_REV; break;
42         case BGR8:  ifmt=GL_RGB;  fmt=GL_BGR;  type=GL_UNSIGNED_BYTE;        break;
43         case BGRA8: ifmt=GL_RGBA; fmt=GL_BGRA; type=GL_UNSIGNED_INT_8_8_8_8_REV; break;
44         default: throw InvalidParameterValue("Invalid texture format");
45         }
46
47         image(level, ifmt, wd, ht, 0, fmt, type, data);
48 }
49
50 /**
51 Loads an image from a file and uploads it into the texture.  Currently assumes
52 the file to be a PNG image.
53 */
54 void Texture2D::image(const string &fn)
55 {
56         FILE *file=fopen(fn.c_str(), "r");
57         if(!file) throw Exception("Couldn't open "+fn);
58
59         char sig[8];
60         fread(sig, 8, 1, file);
61         if(png_sig_cmp((png_byte *)sig, 0, 8))
62                  throw Exception("Not a PNG image");
63
64         png_struct *pngs=png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
65         png_info   *pngi=png_create_info_struct(pngs);
66
67         if(setjmp(png_jmpbuf(pngs)))
68         {
69                 png_destroy_read_struct(&pngs, &pngi, 0);
70                 fclose(file);
71                 throw Exception("PNG error");
72         }
73         png_init_io(pngs, file);
74         png_set_sig_bytes(pngs, 8);
75
76         png_read_info(pngs, pngi);
77
78         unsigned wd=png_get_image_width(pngs, pngi);
79         unsigned ht=png_get_image_height(pngs, pngi);
80         unsigned depth=png_get_bit_depth(pngs, pngi);
81         unsigned ctype=png_get_color_type(pngs, pngi);
82
83         if(ctype==PNG_COLOR_TYPE_PALETTE || depth<8)
84         {
85                 png_destroy_read_struct(&pngs, &pngi, 0);
86                 fclose(file);
87                 throw Exception("Invalid color type or bit depth");
88         }
89
90         if(depth==16)
91                 png_set_strip_16(pngs);
92
93         TextureFormat fmt;
94         unsigned planes;
95
96         switch(ctype)
97         {
98         case PNG_COLOR_TYPE_GRAY: fmt=LUMINANCE8; planes=1; break;
99         case PNG_COLOR_TYPE_GRAY_ALPHA: fmt=LUMINANCE8_ALPHA8; planes=2; break;
100         case PNG_COLOR_TYPE_RGB: fmt=RGB8; planes=3; break;
101         case PNG_COLOR_TYPE_RGB_ALPHA: fmt=RGBA8; planes=4; break;
102         default: throw Exception("Invalid color type");
103         }
104
105         png_byte *data=new png_byte[wd*ht*planes];
106         png_byte *row_ptrs[ht];
107         for(unsigned i=0; i<ht; ++i)
108                 row_ptrs[i]=data+(ht-1-i)*wd*planes;
109
110         png_read_image(pngs, row_ptrs);
111
112         image(0, wd, ht, fmt, data);
113         delete[] data;
114
115         png_destroy_read_struct(&pngs, &pngi, 0);
116         fclose(file);
117 }
118
119 } // namespace GL
120 } // namespace Msp