Debian Codegraph @

utils/fgpanel/FGPNGTextureLoader.cxx

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2011 Torsten Dreyer
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #ifdef HAVE_CONFIG_H
5 # include <config.h>
6 #endif
7
8 #ifdef HAVE_WINDOWS_H
9 #include <windows.h>
10 #endif
11
12 #if defined (SG_MAC)
13 #include <OpenGL/gl.h>
14 #include <GLUT/glut.h>
15 #elif defined (_GLES2)
16 #include <GLES2/gl2.h>
17 #else
18 #include <GL/glew.h> // Must be included before <GL/gl.h>
19 #include <GL/gl.h>
20 #include <GL/glut.h>
21 #endif
22
23 #include <png.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <iostream>
27
28 #include "FGPNGTextureLoader.hxx"
29
30 using namespace std;
31
32 GLuint
33 FGPNGTextureLoader::loadTexture (const string &filename) {
34 //header for testing if it is a png
35 png_byte header[8];
36
37 //open file as binary
38 FILE * const 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 const 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, NULL, NULL));
55 if (!png_ptr) {
56 fclose (fp);
57 return NOTEXTURE;
58 }
59
60 //create png info struct
61 png_infop info_ptr (png_create_info_struct (png_ptr));
62 if (!info_ptr) {
63 png_destroy_read_struct (&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
64 fclose (fp);
65 return NOTEXTURE;
66 }
67
68 //create png info struct
69 png_infop end_info (png_create_info_struct (png_ptr));
70 if (!end_info) {
71 png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp) NULL);
72 fclose (fp);
73 return NOTEXTURE;
74 }
75
76 //png error stuff, not sure libpng man suggests this.
77 if (setjmp (png_jmpbuf (png_ptr))) {
78 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
79 fclose (fp);
80 return NOTEXTURE;
81 }
82
83 // init png reading
84 png_init_io (png_ptr, fp);
85
86 // let libpng know you already read the first 8 bytes
87 png_set_sig_bytes (png_ptr, 8);
88
89 // read all the info up to the image data
90 png_read_info (png_ptr, info_ptr);
91
92 // variables to pass to get info
93 int bit_depth, color_type;
94 png_uint_32 twidth, theight;
95
96 // get info about png
97 png_get_IHDR (png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type, NULL, NULL, NULL);
98
99 // Update the png info struct.
100 png_read_update_info (png_ptr, info_ptr);
101
102 // Row size in bytes.
103 const int rowbytes (png_get_rowbytes (png_ptr, info_ptr));
104
105 // Allocate the image_data as a big block, to be given to opengl
106 png_byte *image_data (new png_byte[rowbytes * theight]);
107 if (!image_data) {
108 //clean up memory and close stuff
109 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
110 fclose (fp);
111 return NOTEXTURE;
112 }
113
114 // row_pointers is for pointing to image_data for reading the png with libpng
115 png_bytep *row_pointers (new png_bytep[theight]);
116 if (!row_pointers) {
117 //clean up memory and close stuff
118 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
119 delete []image_data;
120 fclose (fp);
121 return NOTEXTURE;
122 }
123 // set the individual row_pointers to point at the correct offsets of image_data
124 for (png_uint_32 i = 0; i < theight; ++i) {
125 row_pointers[theight - 1 - i] = image_data + i * rowbytes;
126 }
127 //read the png into image_data through row_pointers
128 png_read_image (png_ptr, row_pointers);
129
130 //Now generate the OpenGL texture object
131 GLuint texture;
132 glGenTextures (1, &texture);
133 glBindTexture (GL_TEXTURE_2D, texture);
134 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, twidth, theight, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) image_data);
135 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
136 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
137
138 //clean up memory and close stuff
139 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
140 delete []image_data;
141 delete []row_pointers;
142 fclose (fp);
143 return texture;
144 }