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