1 //
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License as
4 // published by the Free Software Foundation; either version 2 of the
5 // License, or (at your option) any later version.
6 //
7 // This program is distributed in the hope that it will be useful, but
8 // WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // General Public License for more details.
11 //
12 // You should have received a copy of the GNU General Public License
13 // along with this program; if not, write to the Free Software
14 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 //
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19
20 #ifdef HAVE_WINDOWS_H
21 #include <windows.h>
22 #endif
23
24 #if defined (SG_MAC)
25 #include <OpenGL/gl.h>
26 #include <GLUT/glut.h>
27 #elif defined (_GLES2)
28 #include <GLES2/gl2.h>
29 #else
30 #include <GL/glew.h> // Must be included before <GL/gl.h>
31 #include <GL/gl.h>
32 #include <GL/glut.h>
33 #endif
34
35 #include <png.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <iostream>
39
40 #include "FGPNGTextureLoader.hxx"
41
42 using namespace std;
43
44 GLuint
loadTexture(const string & filename)45 FGPNGTextureLoader::loadTexture (const string &filename) {
46 //header for testing if it is a png
47 png_byte header[8];
48
49 //open file as binary
50 FILE * const fp (fopen (filename.c_str (), "rb"));
51 if (!fp) {
52 return NOTEXTURE;
53 }
54
55 //read the header
56 fread (header, 1, 8, fp);
57
58 //test if png
59 const int is_png (!png_sig_cmp (header, 0, 8));
60 if (!is_png) {
61 fclose (fp);
62 return NOTEXTURE;
63 }
64
65 //create png struct
66 png_structp png_ptr (png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL));
67 if (!png_ptr) {
68 fclose (fp);
69 return NOTEXTURE;
70 }
71
72 //create png info struct
73 png_infop info_ptr (png_create_info_struct (png_ptr));
74 if (!info_ptr) {
75 png_destroy_read_struct (&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
76 fclose (fp);
77 return NOTEXTURE;
78 }
79
80 //create png info struct
81 png_infop end_info (png_create_info_struct (png_ptr));
82 if (!end_info) {
83 png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp) NULL);
84 fclose (fp);
85 return NOTEXTURE;
86 }
87
88 //png error stuff, not sure libpng man suggests this.
89 if (setjmp (png_jmpbuf (png_ptr))) {
90 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
91 fclose (fp);
92 return NOTEXTURE;
93 }
94
95 // init png reading
96 png_init_io (png_ptr, fp);
97
98 // let libpng know you already read the first 8 bytes
99 png_set_sig_bytes (png_ptr, 8);
100
101 // read all the info up to the image data
102 png_read_info (png_ptr, info_ptr);
103
104 // variables to pass to get info
105 int bit_depth, color_type;
106 png_uint_32 twidth, theight;
107
108 // get info about png
109 png_get_IHDR (png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type, NULL, NULL, NULL);
110
111 // Update the png info struct.
112 png_read_update_info (png_ptr, info_ptr);
113
114 // Row size in bytes.
115 const int rowbytes (png_get_rowbytes (png_ptr, info_ptr));
116
117 // Allocate the image_data as a big block, to be given to opengl
118 png_byte *image_data (new png_byte[rowbytes * theight]);
119 if (!image_data) {
120 //clean up memory and close stuff
121 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
122 fclose (fp);
123 return NOTEXTURE;
124 }
125
126 // row_pointers is for pointing to image_data for reading the png with libpng
127 png_bytep *row_pointers (new png_bytep[theight]);
128 if (!row_pointers) {
129 //clean up memory and close stuff
130 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
131 delete []image_data;
132 fclose (fp);
133 return NOTEXTURE;
134 }
135 // set the individual row_pointers to point at the correct offsets of image_data
136 for (png_uint_32 i = 0; i < theight; ++i) {
137 row_pointers[theight - 1 - i] = image_data + i * rowbytes;
138 }
139 //read the png into image_data through row_pointers
140 png_read_image (png_ptr, row_pointers);
141
142 //Now generate the OpenGL texture object
143 GLuint texture;
144 glGenTextures (1, &texture);
145 glBindTexture (GL_TEXTURE_2D, texture);
146 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, twidth, theight, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) image_data);
147 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
148 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
149
150 //clean up memory and close stuff
151 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
152 delete []image_data;
153 delete []row_pointers;
154 fclose (fp);
155 return texture;
156 }
157