]> git.mxchange.org Git - flightgear.git/blob - utils/fgpanel/FGPNGTextureLoader.cxx
let fgpanel compile under vc90
[flightgear.git] / utils / fgpanel / FGPNGTextureLoader.cxx
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 #include "FGPNGTextureLoader.hxx"
24
25 #include <GL/glu.h>
26 #include <png.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include <iostream>
31 using namespace std;
32 GLuint FGPNGTextureLoader::loadTexture( const string & filename )
33 {
34   //header for testing if it is a png
35   png_byte header[8];
36   
37   //open file as binary
38   FILE *fp = fopen(filename.c_str(), "rb");
39   if (!fp) {
40     return NOTEXTURE;
41   }
42   
43   //read the header
44   fread(header, 1, 8, fp);
45   
46   //test if png
47   int is_png = !png_sig_cmp(header, 0, 8);
48   if (!is_png) {
49     fclose(fp);
50     return NOTEXTURE;
51   }
52   
53   //create png struct
54   png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
55       NULL, NULL);
56   if (!png_ptr) {
57     fclose(fp);
58     return (NOTEXTURE);
59   }
60   
61   //create png info struct
62   png_infop info_ptr = png_create_info_struct(png_ptr);
63   if (!info_ptr) {
64     png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
65     fclose(fp);
66     return (NOTEXTURE);
67   }
68
69   //create png info struct
70   png_infop end_info = png_create_info_struct(png_ptr);
71   if (!end_info) {
72     png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
73     fclose(fp);
74     return (NOTEXTURE);
75   }
76
77   //png error stuff, not sure libpng man suggests this.
78   if (setjmp(png_jmpbuf(png_ptr))) {
79     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
80     fclose(fp);
81     return (NOTEXTURE);
82   }
83
84   //init png reading
85   png_init_io(png_ptr, fp);
86   
87   //let libpng know you already read the first 8 bytes
88   png_set_sig_bytes(png_ptr, 8);
89
90   // read all the info up to the image data
91   png_read_info(png_ptr, info_ptr);
92
93   //variables to pass to get info
94   int bit_depth, color_type;
95   png_uint_32 twidth, theight;
96
97   // get info about png
98   png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
99       NULL, NULL, NULL);
100
101   // Update the png info struct.
102   png_read_update_info(png_ptr, info_ptr);
103
104   // Row size in bytes.
105   int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
106
107   // Allocate the image_data as a big block, to be given to opengl
108   png_byte *image_data = new png_byte[rowbytes * theight];
109   if (!image_data) {
110     //clean up memory and close stuff
111     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
112     fclose(fp);
113     return NOTEXTURE;
114   }
115
116   //row_pointers is for pointing to image_data for reading the png with libpng
117   png_bytep *row_pointers = new png_bytep[theight];
118   if (!row_pointers) {
119     //clean up memory and close stuff
120     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
121     delete[] image_data;
122     fclose(fp);
123     return NOTEXTURE;
124   }
125   // set the individual row_pointers to point at the correct offsets of image_data
126   for (png_uint_32 i = 0; i < theight; ++i)
127     row_pointers[theight - 1 - i] = image_data + i * rowbytes;
128
129   //read the png into image_data through row_pointers
130   png_read_image(png_ptr, row_pointers);
131
132   //Now generate the OpenGL texture object
133   GLuint texture;
134   glGenTextures(1, &texture);
135   glBindTexture(GL_TEXTURE_2D, texture);
136   gluBuild2DMipmaps( GL_TEXTURE_2D, 4, twidth, theight, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)image_data );
137   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
138   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR );
139 //  glTexImage2D(GL_TEXTURE_2D,0, GL_RGBA, twidth, theight, 0,
140 //      GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) image_data);
141 //  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
142
143   //clean up memory and close stuff
144   png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
145   delete[] image_data;
146   delete[] row_pointers;
147   fclose(fp);
148   return texture;
149 }