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