ARB
glpng.c
Go to the documentation of this file.
1 /* PNG loader library for OpenGL v1.45 (10/07/00)
2  * by Ben Wyatt ben@wyatt100.freeserve.co.uk
3  * Using LibPNG 1.0.2 and ZLib 1.1.3
4  *
5  * This software is provided 'as-is', without any express or implied warranty.
6  * In no event will the author be held liable for any damages arising from the
7  * use of this software.
8  *
9  * Permission is hereby granted to use, copy, modify, and distribute this
10  * source code, or portions hereof, for any purpose, without fee, subject to
11  * the following restrictions:
12  *
13  * 1. The origin of this source code must not be misrepresented. You must not
14  * claim that you wrote the original software. If you use this software in
15  * a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered versions must be plainly marked as such and must not be
18  * misrepresented as being the original source.
19  * 3. This notice must not be removed or altered from any source distribution.
20  *
21  * ---------------------------------------------------
22  * This version has been modified for usage inside ARB
23  * http://arb-home.de/
24  */
25 
26 #ifdef _WIN32 // Stupid Windows needs to include windows.h before gl.h
27 #undef FAR
28 #include <windows.h>
29 #endif
30 
31 #include <GL/glew.h>
32 #include <GL/gl.h>
33 // #include <GL/glext.h>
34 // "fix" conflicting types defined via GL/glew.h and GL/glext.h (e.g. PFNGLFRAGMENTLIGHTFVSGIXPROC)
35 #include <GL/glpng.h>
36 #include <stdlib.h>
37 #include <math.h>
38 #include <png.h>
39 
40 #if 0
41 #define GLPNG_CHECK_SIG(header,size) png_check_sig(header,size) // old libpng
42 #else
43 #define GLPNG_CHECK_SIG(header,size) (png_sig_cmp(header,0,size)==0)
44 #endif
45 
46 // Used to decide if GL/gl.h supports the paletted extension
47 #ifdef GL_COLOR_INDEX1_EXT
48 #define SUPPORTS_PALETTE_EXT
49 #endif
50 
51 #pragma GCC diagnostic ignored "-Wunused-parameter"
52 static unsigned char DefaultAlphaCallback(unsigned char red, unsigned char green, unsigned char blue) {
53  return 255;
54 }
55 
56 static unsigned char StencilRed = 0, StencilGreen = 0, StencilBlue = 0;
57 static unsigned char (*AlphaCallback)(unsigned char red, unsigned char green, unsigned char blue) = DefaultAlphaCallback;
58 static int StandardOrientation = 0;
59 
60 #ifdef SUPPORTS_PALETTE_EXT
61 #ifdef _WIN32
62 static PFNGLCOLORTABLEEXTPROC glColorTableEXT = NULL;
63 #endif
64 #endif
65 
66 static int PalettedTextures = -1;
67 static GLint MaxTextureSize = 0;
68 
69 /* screenGamma = displayGamma/viewingGamma
70  * displayGamma = CRT has gamma of ~2.2
71  * viewingGamma depends on platform. PC is 1.0, Mac is 1.45,
72  * but this can be checked and changed w/ /usr/sbin/gamma command.
73  * If the environment variable VIEWING_GAMMA is set, adjust gamma per this value.
74  */
75 #ifdef _MAC
76 static double screenGamma = 2.2 / 1.45;
77 #else // PC/default
78 static double screenGamma = 2.2 / 1.0;
79 #endif
80 
81 static char gammaExplicit = 0; // if
82 
83 static void checkForGammaEnv()
84 {
85  double viewingGamma;
86  char *gammaEnv = getenv("VIEWING_GAMMA");
87 
88  if (gammaEnv && !gammaExplicit)
89  {
90  sscanf(gammaEnv, "%lf", &viewingGamma);
91  screenGamma = 2.2/viewingGamma;
92  }
93 }
94 
95 // Returns a safe texture size to use (ie a power of 2), based on the current texture size "i"
96 static int SafeSize(int i) {
97  int p;
98 
99  if (i > MaxTextureSize) return MaxTextureSize;
100 
101  for (p = 0; p < 24; p++)
102  if (i <= (1<<p))
103  return 1<<p;
104 
105  return MaxTextureSize;
106 }
107 
108 // Resize the texture since gluScaleImage doesn't work on everything
109 static void Resize(int components, const png_bytep d1, int w1, int h1, png_bytep d2, int w2, int h2) {
110  const float sx = (float) w1/w2, sy = (float) h1/h2;
111  int x, y, xx, yy, c;
112  png_bytep d;
113 
114  for (y = 0; y < h2; y++) {
115  yy = (int) (y*sy)*w1;
116 
117  for (x = 0; x < w2; x++) {
118  xx = (int) (x*sx);
119  d = d1 + (yy+xx)*components;
120 
121  for (c = 0; c < components; c++)
122  *d2++ = *d++;
123  }
124  }
125 }
126 
127 #ifdef SUPPORTS_PALETTE_EXT
128 #ifdef _WIN32
129 static int ExtSupported(const char *x) {
130  static const GLubyte *ext = NULL;
131  const char *c;
132  int xlen = strlen(x);
133 
134  if (ext == NULL) ext = glGetString(GL_EXTENSIONS);
135 
136  c = (const char*)ext;
137 
138  while (*c != '\0') {
139  if (strcmp(c, x) == 0 && (c[xlen] == '\0' || c[xlen] == ' ')) return 1;
140  c++;
141  }
142 
143  return 0;
144 }
145 #endif
146 #endif
147 
148 #define GET(o) ((int)*(data + (o)))
149 
150 static int HalfSize(GLint components, GLint width, GLint height, const unsigned char *data, unsigned char *d, int filter) {
151  int x, y, c;
152  int line = width*components;
153 
154  if (width > 1 && height > 1) {
155  if (filter)
156  for (y = 0; y < height; y += 2) {
157  for (x = 0; x < width; x += 2) {
158  for (c = 0; c < components; c++) {
159  *d++ = (GET(0)+GET(components)+GET(line)+GET(line+components)) / 4;
160  data++;
161  }
162  data += components;
163  }
164  data += line;
165  }
166  else
167  for (y = 0; y < height; y += 2) {
168  for (x = 0; x < width; x += 2) {
169  for (c = 0; c < components; c++) {
170  *d++ = GET(0);
171  data++;
172  }
173  data += components;
174  }
175  data += line;
176  }
177  }
178  else if (width > 1 && height == 1) {
179  if (filter)
180  for (y = 0; y < height; y += 1) {
181  for (x = 0; x < width; x += 2) {
182  for (c = 0; c < components; c++) {
183  *d++ = (GET(0)+GET(components)) / 2;
184  data++;
185  }
186  data += components;
187  }
188  }
189  else
190  for (y = 0; y < height; y += 1) {
191  for (x = 0; x < width; x += 2) {
192  for (c = 0; c < components; c++) {
193  *d++ = GET(0);
194  data++;
195  }
196  data += components;
197  }
198  }
199  }
200  else if (width == 1 && height > 1) {
201  if (filter)
202  for (y = 0; y < height; y += 2) {
203  for (x = 0; x < width; x += 1) {
204  for (c = 0; c < components; c++) {
205  *d++ = (GET(0)+GET(line)) / 2;
206  data++;
207  }
208  }
209  data += line;
210  }
211  else
212  for (y = 0; y < height; y += 2) {
213  for (x = 0; x < width; x += 1) {
214  for (c = 0; c < components; c++) {
215  *d++ = GET(0);
216  data++;
217  }
218  }
219  data += line;
220  }
221  }
222  else {
223  return 0;
224  }
225 
226  return 1;
227 }
228 
229 #undef GET
230 
231 // Replacement for gluBuild2DMipmaps so GLU isn't needed
232 static void Build2DMipmaps(GLint components, GLint width, GLint height, GLenum format, const unsigned char *data, int filter) {
233  int level = 0;
234  unsigned char *d = (unsigned char *) malloc((width/2)*(height/2)*components+4);
235  const unsigned char *last = data;
236 
237  glTexImage2D(GL_TEXTURE_2D, level, components, width, height, 0, format, GL_UNSIGNED_BYTE, data);
238  level++;
239 
240  while (HalfSize(components, width, height, last, d, filter)) {
241  if (width > 1) width /= 2;
242  if (height > 1) height /= 2;
243 
244  glTexImage2D(GL_TEXTURE_2D, level, components, width, height, 0, format, GL_UNSIGNED_BYTE, d);
245  level++;
246  last = d;
247  }
248 
249  free(d);
250 }
251 
252 static int APIENTRY pngLoadF(FILE *fp, int mipmap, int trans, pngInfo *pinfo) {
253  unsigned char header[8];
254 
255  if (fread(header, 1, 8, fp) != 8) return 0;
256  if (!GLPNG_CHECK_SIG(header, 8)) return 0;
257 
258  GLint pack, unpack;
259  png_structp png;
260  png_infop info;
261  png_infop endinfo;
262  png_bytep data;
263  png_bytep *row_p;
264  double fileGamma;
265 
266  png_uint_32 width, height, rw, rh;
267  int depth, color;
268 
269  png_uint_32 i;
270 
271  png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
272  info = png_create_info_struct(png);
273  endinfo = png_create_info_struct(png);
274 
275  // DH: added following lines
276  if (setjmp(png_jmpbuf(png))) {
277  png_destroy_read_struct(&png, &info, &endinfo);
278  return 0;
279  }
280  // ~DH
281 
282  png_init_io(png, fp);
283  png_set_sig_bytes(png, 8);
284  png_read_info(png, info);
285  png_get_IHDR(png, info, &width, &height, &depth, &color, NULL, NULL, NULL);
286 
287  if (pinfo != NULL) {
288  pinfo->Width = width;
289  pinfo->Height = height;
290  pinfo->Depth = depth;
291  }
292 
293  if (MaxTextureSize == 0)
294  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &MaxTextureSize);
295 
296 #ifdef SUPPORTS_PALETTE_EXT
297 #ifdef _WIN32
298  if (PalettedTextures == -1)
299  PalettedTextures = ExtSupported("GL_EXT_paletted_texture") && (strstr((const char *) glGetString(GL_VERSION), "1.1.0 3Dfx Beta") == NULL);
300 
301  if (PalettedTextures) {
302  if (glColorTableEXT == NULL) {
303  glColorTableEXT = (PFNGLCOLORTABLEEXTPROC) wglGetProcAddress("glColorTableEXT");
304  if (glColorTableEXT == NULL)
305  PalettedTextures = 0;
306  }
307  }
308 #endif
309 #endif
310 
311  if (PalettedTextures == -1)
312  PalettedTextures = 0;
313 
314  if (color == PNG_COLOR_TYPE_GRAY || color == PNG_COLOR_TYPE_GRAY_ALPHA)
315  png_set_gray_to_rgb(png);
316 
317  if (color&PNG_COLOR_MASK_ALPHA && trans != GLPNG_ALPHA) {
318  png_set_strip_alpha(png);
319  color &= ~PNG_COLOR_MASK_ALPHA;
320  }
321 
322  if (!(PalettedTextures && mipmap >= 0 && trans == GLPNG_SOLID))
323  if (color == PNG_COLOR_TYPE_PALETTE)
324  png_set_expand(png);
325 
326  // --GAMMA--
328  if (png_get_gAMA(png, info, &fileGamma))
329  png_set_gamma(png, screenGamma, fileGamma);
330  else
331  png_set_gamma(png, screenGamma, 1.0/2.2);
332 
333  png_read_update_info(png, info);
334 
335  data = (png_bytep) malloc(png_get_rowbytes(png, info)*height);
336  row_p = (png_bytep *) malloc(sizeof(png_bytep)*height);
337 
338  for (i = 0; i < height; i++) {
339  if (StandardOrientation)
340  row_p[height - 1 - i] = &data[png_get_rowbytes(png, info)*i];
341  else
342  row_p[i] = &data[png_get_rowbytes(png, info)*i];
343  }
344 
345  png_read_image(png, row_p);
346  free(row_p);
347 
348  rw = SafeSize(width), rh = SafeSize(height);
349 
350  if (rw != width || rh != height) {
351  const int channels = png_get_rowbytes(png, info)/width;
352 
353  png_bytep data2 = (png_bytep) malloc(rw*rh*channels);
354 
355  // Doesn't work on certain sizes
356  /* if (gluScaleImage(glformat, width, height, GL_UNSIGNED_BYTE, data, rw, rh, GL_UNSIGNED_BYTE, data2) != 0)
357  return 0;
358  */
359  Resize(channels, data, width, height, data2, rw, rh);
360 
361  width = rw, height = rh;
362  free(data);
363  data = data2;
364  }
365 
366  { // OpenGL stuff
367  glGetIntegerv(GL_PACK_ALIGNMENT, &pack);
368  glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpack);
369  glPixelStorei(GL_PACK_ALIGNMENT, 1);
370  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
371 
372 #ifdef SUPPORTS_PALETTE_EXT
373  if (PalettedTextures && mipmap >= 0 && trans == GLPNG_SOLID && color == PNG_COLOR_TYPE_PALETTE) {
374  png_colorp pal;
375  int cols;
376  GLint internalFormat;
377 
378  if (pinfo != NULL) pinfo->Alpha = 0;
379  png_get_PLTE(png, info, &pal, &cols);
380 
381  switch (cols) {
382  case 1<<1: internalFormat = GL_COLOR_INDEX1_EXT; break;
383  case 1<<2: internalFormat = GL_COLOR_INDEX2_EXT; break;
384  case 1<<4: internalFormat = GL_COLOR_INDEX4_EXT; break;
385  case 1<<8: internalFormat = GL_COLOR_INDEX8_EXT; break;
386  case 1<<12: internalFormat = GL_COLOR_INDEX12_EXT; break;
387  case 1<<16: internalFormat = GL_COLOR_INDEX16_EXT; break;
388  default:
389  return 0;
390  }
391  glColorTableEXT(GL_TEXTURE_2D, GL_RGB8, cols, GL_RGB, GL_UNSIGNED_BYTE, pal);
392  glTexImage2D(GL_TEXTURE_2D, mipmap, internalFormat, width, height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, data);
393  }
394  else
395 #endif
396  if (trans == GLPNG_SOLID || trans == GLPNG_ALPHA || color == PNG_COLOR_TYPE_RGB_ALPHA || color == PNG_COLOR_TYPE_GRAY_ALPHA) {
397  GLenum glformat;
398  GLint glcomponent;
399 
400  switch (color) {
401  case PNG_COLOR_TYPE_GRAY:
402  case PNG_COLOR_TYPE_RGB:
403  case PNG_COLOR_TYPE_PALETTE:
404  glformat = GL_RGB;
405  glcomponent = 3;
406  if (pinfo != NULL) pinfo->Alpha = 0;
407  break;
408 
409  case PNG_COLOR_TYPE_GRAY_ALPHA:
410  case PNG_COLOR_TYPE_RGB_ALPHA:
411  glformat = GL_RGBA;
412  glcomponent = 4;
413  if (pinfo != NULL) pinfo->Alpha = 8;
414  break;
415 
416  default:
417  return 0;
418  }
419 
420  if (mipmap == GLPNG_BUILDMIPMAPS)
421  Build2DMipmaps(glcomponent, width, height, glformat, data, 1);
422  else if (mipmap == GLPNG_SIMPLEMIPMAPS)
423  Build2DMipmaps(glcomponent, width, height, glformat, data, 0);
424  else
425  glTexImage2D(GL_TEXTURE_2D, mipmap, glcomponent, width, height, 0, glformat, GL_UNSIGNED_BYTE, data);
426  }
427  else {
428  png_bytep p, endp, q, data2;
429  int r, g, b, a;
430 
431  p = data, endp = p+width*height*3;
432  q = data2 = (png_bytep) malloc(sizeof(png_byte)*width*height*4);
433 
434  if (pinfo != NULL) pinfo->Alpha = 8;
435 
436 #define FORSTART \
437  do { \
438  r = *p++; /* red */ \
439  g = *p++; /* green */ \
440  b = *p++; /* blue */ \
441  *q++ = r; \
442  *q++ = g; \
443  *q++ = b;
444 
445 #define FOREND \
446  q++; \
447  } while (p != endp);
448 
449 #define ALPHA *q
450 
451  switch (trans) {
452  case GLPNG_CALLBACK:
453  FORSTART
454  ALPHA = AlphaCallback((unsigned char) r, (unsigned char) g, (unsigned char) b);
455  FOREND
456  break;
457 
458  case GLPNG_STENCIL:
459  FORSTART
460  if (r == StencilRed && g == StencilGreen && b == StencilBlue)
461  ALPHA = 0;
462  else
463  ALPHA = 255;
464  FOREND
465  break;
466 
467  case GLPNG_BLEND1:
468  FORSTART
469  a = r+g+b;
470  if (a > 255) ALPHA = 255; else ALPHA = a;
471  FOREND
472  break;
473 
474  case GLPNG_BLEND2:
475  FORSTART
476  a = r+g+b;
477  if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
478  FOREND
479  break;
480 
481  case GLPNG_BLEND3:
482  FORSTART
483  ALPHA = (r+g+b)/3;
484  FOREND
485  break;
486 
487  case GLPNG_BLEND4:
488  FORSTART
489  a = r*r+g*g+b*b;
490  if (a > 255) ALPHA = 255; else ALPHA = a;
491  FOREND
492  break;
493 
494  case GLPNG_BLEND5:
495  FORSTART
496  a = r*r+g*g+b*b;
497  if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
498  FOREND
499  break;
500 
501  case GLPNG_BLEND6:
502  FORSTART
503  a = r*r+g*g+b*b;
504  if (a > 255*3) ALPHA = 255; else ALPHA = a/3;
505  FOREND
506  break;
507 
508  case GLPNG_BLEND7:
509  FORSTART
510  a = r*r+g*g+b*b;
511  if (a > 255*255) ALPHA = 255; else ALPHA = (int) sqrt(a);
512  FOREND
513  break;
514  }
515 
516 #undef FORSTART
517 #undef FOREND
518 #undef ALPHA
519 
520  if (mipmap == GLPNG_BUILDMIPMAPS)
521  Build2DMipmaps(4, width, height, GL_RGBA, data2, 1);
522  else if (mipmap == GLPNG_SIMPLEMIPMAPS)
523  Build2DMipmaps(4, width, height, GL_RGBA, data2, 0);
524  else
525  glTexImage2D(GL_TEXTURE_2D, mipmap, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);
526 
527  free(data2);
528  }
529 
530  glPixelStorei(GL_PACK_ALIGNMENT, pack);
531  glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
532  } // OpenGL end
533 
534  png_read_end(png, endinfo);
535  png_destroy_read_struct(&png, &info, &endinfo);
536 
537  free(data);
538 
539  return 1;
540 }
541 
542 static int APIENTRY pngLoad(const char *filename, int mipmap, int trans, pngInfo *pinfo) {
543  int result;
544  FILE *fp = fopen(filename, "rb");
545  if (fp == NULL) return 0;
546 
547  result = pngLoadF(fp, mipmap, trans, pinfo);
548 
549  if (fclose(fp) != 0) return 0;
550 
551  return result;
552 }
553 
554 static unsigned int SetParams(int wrapst, int magfilter, int minfilter) {
555  unsigned int id;
556 
557  glGenTextures(1, &id);
558  glBindTexture(GL_TEXTURE_2D, id);
559 
560  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapst);
561  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapst);
562 
563  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter);
564  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter);
565 
566  return id;
567 }
568 
569 unsigned int APIENTRY pngBind(const char *filename, int mipmap, int trans, pngInfo *info, int wrapst, int minfilter, int magfilter) {
570  unsigned int id = SetParams(wrapst, magfilter, minfilter);
571 
572  if (id != 0 && pngLoad(filename, mipmap, trans, info))
573  return id;
574  return 0;
575 }
576 
static void Resize(int components, const png_bytep d1, int w1, int h1, png_bytep d2, int w2, int h2)
Definition: glpng.c:109
#define GLPNG_BLEND3
Definition: glpng.h:69
string result
static int SafeSize(int i)
Definition: glpng.c:96
#define GLPNG_SIMPLEMIPMAPS
Definition: glpng.h:55
static char gammaExplicit
Definition: glpng.c:81
const char * id
Definition: AliAdmin.cxx:17
#define GLPNG_STENCIL
Definition: glpng.h:66
AliDataPtr format(AliDataPtr data, const size_t wanted_len, GB_ERROR &error)
Definition: insdel.cxx:615
#define GET(o)
Definition: glpng.c:148
#define GLPNG_BLEND4
Definition: glpng.h:70
static char * y[maxsp+1]
static int PalettedTextures
Definition: glpng.c:66
static GLint MaxTextureSize
Definition: glpng.c:67
#define GLPNG_BLEND1
Definition: glpng.h:67
#define GLPNG_BLEND2
Definition: glpng.h:68
#define GLPNG_SOLID
Definition: glpng.h:65
static unsigned char(* AlphaCallback)(unsigned char red, unsigned char green, unsigned char blue)
Definition: glpng.c:57
#define GLPNG_BUILDMIPMAPS
Definition: glpng.h:54
static unsigned char StencilGreen
Definition: glpng.c:56
#define GLPNG_CALLBACK
Definition: glpng.h:63
unsigned int APIENTRY pngBind(const char *filename, int mipmap, int trans, pngInfo *info, int wrapst, int minfilter, int magfilter)
Definition: glpng.c:569
static void Build2DMipmaps(GLint components, GLint width, GLint height, GLenum format, const unsigned char *data, int filter)
Definition: glpng.c:232
#define FOREND
static int APIENTRY pngLoad(const char *filename, int mipmap, int trans, pngInfo *pinfo)
Definition: glpng.c:542
#define FORSTART
static double screenGamma
Definition: glpng.c:78
unsigned int Alpha
Definition: glpng.h:80
unsigned int Height
Definition: glpng.h:78
#define GLPNG_BLEND7
Definition: glpng.h:73
static int HalfSize(GLint components, GLint width, GLint height, const unsigned char *data, unsigned char *d, int filter)
Definition: glpng.c:150
#define GLPNG_ALPHA
Definition: glpng.h:64
static void checkForGammaEnv()
Definition: glpng.c:83
Definition: glpng.h:76
static int StandardOrientation
Definition: glpng.c:58
static unsigned char StencilRed
Definition: glpng.c:56
unsigned int Width
Definition: glpng.h:77
unsigned int Depth
Definition: glpng.h:79
static int APIENTRY pngLoadF(FILE *fp, int mipmap, int trans, pngInfo *pinfo)
Definition: glpng.c:252
static int line
Definition: arb_a2ps.c:296
#define APIENTRY
Definition: glpng.h:48
#define GLPNG_BLEND6
Definition: glpng.h:72
#define ALPHA
#define GLPNG_BLEND5
Definition: glpng.h:71
static unsigned char DefaultAlphaCallback(unsigned char red, unsigned char green, unsigned char blue)
Definition: glpng.c:52
static int info[maxsites+1]
static unsigned char StencilBlue
Definition: glpng.c:56
static unsigned int SetParams(int wrapst, int magfilter, int minfilter)
Definition: glpng.c:554
#define GLPNG_CHECK_SIG(header, size)
Definition: glpng.c:43