~ubuntu-branches/ubuntu/precise/flightgear/precise

« back to all changes in this revision

Viewing changes to utils/fgpanel/FGPNGTextureLoader.cxx

  • Committer: Package Import Robot
  • Author(s): Ove Kaaven
  • Date: 2011-09-03 22:16:12 UTC
  • mfrom: (3.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20110903221612-2cjy0z7ztj5nkln5
Tags: 2.4.0-1
* New upstream release. Closes: #638588.
* Build-Depend on OpenSceneGraph 3.0, and the Subversion library.
* Recommend fgfs-scenery-base.
* Enable parallel builds (shorter compile times on multicore CPUs).
* Removed hack that tried to build without optimizations if
  building with optimizations fails.

Show diffs side-by-side

added added

removed removed

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