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