2 * PNG loader library for OpenGL v1.45 (10/07/00)
3 * by Ben Wyatt ben@wyatt100.freeserve.co.uk
4 * Using LibPNG 1.0.2 and ZLib 1.1.3
6 * This software is provided 'as-is', without any express or implied warranty.
7 * In no event will the author be held liable for any damages arising from the
8 * use of this software.
10 * Permission is hereby granted to use, copy, modify, and distribute this
11 * source code, or portions hereof, for any purpose, without fee, subject to
12 * the following restrictions:
14 * 1. The origin of this source code must not be misrepresented. You must not
15 * claim that you wrote the original software. If you use this software in
16 * a product, an acknowledgment in the product documentation would be
17 * appreciated but is not required.
18 * 2. Altered versions must be plainly marked as such and must not be
19 * misrepresented as being the original source.
20 * 3. This notice must not be removed or altered from any source distribution.
23 #ifdef _WIN32 /* Stupid Windows needs to include windows.h before gl.h */
34 /* Used to decide if GL/gl.h supports the paletted extension */
35 #ifdef GL_COLOR_INDEX1_EXT
36 #define SUPPORTS_PALETTE_EXT
39 static unsigned char DefaultAlphaCallback(unsigned char red
, unsigned char green
, unsigned char blue
) {
43 static unsigned char StencilRed
= 0, StencilGreen
= 0, StencilBlue
= 0;
44 static unsigned char (*AlphaCallback
)(unsigned char red
, unsigned char green
, unsigned char blue
) = DefaultAlphaCallback
;
45 static int StandardOrientation
= 0;
47 #ifdef SUPPORTS_PALETTE_EXT
49 static PFNGLCOLORTABLEEXTPROC glColorTableEXT
= NULL
;
53 static int PalettedTextures
= -1;
54 static GLint MaxTextureSize
= 0;
56 /* screenGamma = displayGamma/viewingGamma
57 * displayGamma = CRT has gamma of ~2.2
58 * viewingGamma depends on platform. PC is 1.0, Mac is 1.45, SGI defaults
59 * to 1.7, but this can be checked and changed w/ /usr/sbin/gamma command.
60 * If the environment variable VIEWING_GAMMA is set, adjust gamma per this value.
63 static double screenGamma
= 2.2 / 1.45;
65 static double screenGamma
= 2.2 / 1.7;
66 #else /* PC/default */
67 static double screenGamma
= 2.2 / 1.0;
70 static char gammaExplicit
= 0; /*if */
72 static void checkForGammaEnv()
75 char *gammaEnv
= getenv("VIEWING_GAMMA");
77 if(gammaEnv
&& !gammaExplicit
)
79 sscanf(gammaEnv
, "%lf", &viewingGamma
);
80 screenGamma
= 2.2/viewingGamma
;
84 /* Returns a safe texture size to use (ie a power of 2), based on the current texture size "i" */
85 static int SafeSize(int i
) {
88 if (i
> MaxTextureSize
) return MaxTextureSize
;
90 for (p
= 0; p
< 24; p
++)
94 return MaxTextureSize
;
97 /* Resize the texture since gluScaleImage doesn't work on everything */
98 static void Resize(int components
, const png_bytep d1
, int w1
, int h1
, png_bytep d2
, int w2
, int h2
) {
99 const float sx
= (float) w1
/w2
, sy
= (float) h1
/h2
;
103 for (y
= 0; y
< h2
; y
++) {
104 yy
= (int) (y
*sy
)*w1
;
106 for (x
= 0; x
< w2
; x
++) {
108 d
= d1
+ (yy
+xx
)*components
;
110 for (c
= 0; c
< components
; c
++)
116 static int ExtSupported(const char *x
) {
117 static const GLubyte
*ext
= NULL
;
119 int xlen
= strlen(x
);
121 if (ext
== NULL
) ext
= glGetString(GL_EXTENSIONS
);
123 c
= (const char*)ext
;
126 if (strcmp(c
, x
) == 0 && (c
[xlen
] == '\0' || c
[xlen
] == ' ')) return 1;
133 #define GET(o) ((int)*(data + (o)))
135 static int HalfSize(GLint components
, GLint width
, GLint height
, const unsigned char *data
, unsigned char *d
, int filter
) {
137 int line
= width
*components
;
139 if (width
> 1 && height
> 1) {
141 for (y
= 0; y
< height
; y
+= 2) {
142 for (x
= 0; x
< width
; x
+= 2) {
143 for (c
= 0; c
< components
; c
++) {
144 *d
++ = (GET(0)+GET(components
)+GET(line
)+GET(line
+components
)) / 4;
152 for (y
= 0; y
< height
; y
+= 2) {
153 for (x
= 0; x
< width
; x
+= 2) {
154 for (c
= 0; c
< components
; c
++) {
163 else if (width
> 1 && height
== 1) {
165 for (y
= 0; y
< height
; y
+= 1) {
166 for (x
= 0; x
< width
; x
+= 2) {
167 for (c
= 0; c
< components
; c
++) {
168 *d
++ = (GET(0)+GET(components
)) / 2;
175 for (y
= 0; y
< height
; y
+= 1) {
176 for (x
= 0; x
< width
; x
+= 2) {
177 for (c
= 0; c
< components
; c
++) {
185 else if (width
== 1 && height
> 1) {
187 for (y
= 0; y
< height
; y
+= 2) {
188 for (x
= 0; x
< width
; x
+= 1) {
189 for (c
= 0; c
< components
; c
++) {
190 *d
++ = (GET(0)+GET(line
)) / 2;
197 for (y
= 0; y
< height
; y
+= 2) {
198 for (x
= 0; x
< width
; x
+= 1) {
199 for (c
= 0; c
< components
; c
++) {
216 /* Replacement for gluBuild2DMipmaps so GLU isn't needed */
217 static void Build2DMipmaps(GLint components
, GLint width
, GLint height
, GLenum format
, const unsigned char *data
, int filter
) {
219 unsigned char *d
= (unsigned char *) malloc((width
/2)*(height
/2)*components
+4);
220 const unsigned char *last
= data
;
222 glTexImage2D(GL_TEXTURE_2D
, level
, components
, width
, height
, 0, format
, GL_UNSIGNED_BYTE
, data
);
225 while (HalfSize(components
, width
, height
, last
, d
, filter
)) {
226 if (width
> 1) width
/= 2;
227 if (height
> 1) height
/= 2;
229 glTexImage2D(GL_TEXTURE_2D
, level
, components
, width
, height
, 0, format
, GL_UNSIGNED_BYTE
, d
);
237 int APIENTRY
pngLoadRaw(const char *filename
, pngRawInfo
*pinfo
) {
239 FILE *fp
= fopen(filename
, "rb");
240 if (fp
== NULL
) return 0;
242 result
= pngLoadRawF(fp
, pinfo
);
244 if (fclose(fp
) != 0) {
247 free(pinfo
->Palette
);
255 int APIENTRY
pngLoadRawF(FILE *fp
, pngRawInfo
*pinfo
) {
256 unsigned char header
[8];
264 png_uint_32 width
, height
;
269 if (pinfo
== NULL
) return 0;
271 fread(header
, 1, 8, fp
);
272 if (!png_check_sig(header
, 8)) return 0;
274 png
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
275 info
= png_create_info_struct(png
);
276 endinfo
= png_create_info_struct(png
);
278 // DH: added following lines
279 if (setjmp(png
->jmpbuf
))
281 png_destroy_read_struct(&png
, &info
, &endinfo
);
286 png_init_io(png
, fp
);
287 png_set_sig_bytes(png
, 8);
288 png_read_info(png
, info
);
289 png_get_IHDR(png
, info
, &width
, &height
, &depth
, &color
, NULL
, NULL
, NULL
);
291 pinfo
->Width
= width
;
292 pinfo
->Height
= height
;
293 pinfo
->Depth
= depth
;
297 if (png_get_gAMA(png
, info
, &fileGamma
))
298 png_set_gamma(png
, screenGamma
, fileGamma
);
300 png_set_gamma(png
, screenGamma
, 1.0/2.2);
302 png_read_update_info(png
, info
);
304 data
= (png_bytep
) malloc(png_get_rowbytes(png
, info
)*height
);
305 row_p
= (png_bytep
*) malloc(sizeof(png_bytep
)*height
);
307 for (i
= 0; i
< height
; i
++) {
308 if (StandardOrientation
)
309 row_p
[height
- 1 - i
] = &data
[png_get_rowbytes(png
, info
)*i
];
311 row_p
[i
] = &data
[png_get_rowbytes(png
, info
)*i
];
314 png_read_image(png
, row_p
);
317 if (color
== PNG_COLOR_TYPE_PALETTE
) {
319 png_get_PLTE(png
, info
, (png_colorp
*) &pinfo
->Palette
, &cols
);
322 pinfo
->Palette
= NULL
;
325 if (color
&PNG_COLOR_MASK_ALPHA
) {
326 if (color
&PNG_COLOR_MASK_PALETTE
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
)
327 pinfo
->Components
= 2;
329 pinfo
->Components
= 4;
333 if (color
&PNG_COLOR_MASK_PALETTE
|| color
== PNG_COLOR_TYPE_GRAY
)
334 pinfo
->Components
= 1;
336 pinfo
->Components
= 3;
342 png_read_end(png
, endinfo
);
343 png_destroy_read_struct(&png
, &info
, &endinfo
);
348 int APIENTRY
pngLoad(const char *filename
, int mipmap
, int trans
, pngInfo
*pinfo
) {
350 FILE *fp
= fopen(filename
, "rb");
351 if (fp
== NULL
) return 0;
353 result
= pngLoadF(fp
, mipmap
, trans
, pinfo
);
355 if (fclose(fp
) != 0) return 0;
360 int APIENTRY
pngLoadF(FILE *fp
, int mipmap
, int trans
, pngInfo
*pinfo
) {
362 unsigned char header
[8];
366 png_bytep data
, data2
;
370 png_uint_32 width
, height
, rw
, rh
;
375 fread(header
, 1, 8, fp
);
376 if (!png_check_sig(header
, 8)) return 0;
378 png
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
379 info
= png_create_info_struct(png
);
380 endinfo
= png_create_info_struct(png
);
382 // DH: added following lines
383 if (setjmp(png
->jmpbuf
))
385 png_destroy_read_struct(&png
, &info
, &endinfo
);
390 png_init_io(png
, fp
);
391 png_set_sig_bytes(png
, 8);
392 png_read_info(png
, info
);
393 png_get_IHDR(png
, info
, &width
, &height
, &depth
, &color
, NULL
, NULL
, NULL
);
396 pinfo
->Width
= width
;
397 pinfo
->Height
= height
;
398 pinfo
->Depth
= depth
;
401 if (MaxTextureSize
== 0)
402 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, &MaxTextureSize
);
404 #ifdef SUPPORTS_PALETTE_EXT
406 if (PalettedTextures
== -1)
407 PalettedTextures
= ExtSupported("GL_EXT_paletted_texture") && (strstr((const char *) glGetString(GL_VERSION
), "1.1.0 3Dfx Beta") == NULL
);
409 if (PalettedTextures
) {
410 if (glColorTableEXT
== NULL
) {
411 glColorTableEXT
= (PFNGLCOLORTABLEEXTPROC
) wglGetProcAddress("glColorTableEXT");
412 if (glColorTableEXT
== NULL
)
413 PalettedTextures
= 0;
419 if (PalettedTextures
== -1)
420 PalettedTextures
= 0;
422 if (color
== PNG_COLOR_TYPE_GRAY
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
)
423 png_set_gray_to_rgb(png
);
425 if (color
&PNG_COLOR_MASK_ALPHA
&& trans
!= PNG_ALPHA
) {
426 png_set_strip_alpha(png
);
427 color
&= ~PNG_COLOR_MASK_ALPHA
;
430 if (!(PalettedTextures
&& mipmap
>= 0 && trans
== PNG_SOLID
))
431 if (color
== PNG_COLOR_TYPE_PALETTE
)
436 if (png_get_gAMA(png
, info
, &fileGamma
))
437 png_set_gamma(png
, screenGamma
, fileGamma
);
439 png_set_gamma(png
, screenGamma
, 1.0/2.2);
441 png_read_update_info(png
, info
);
443 data
= (png_bytep
) malloc(png_get_rowbytes(png
, info
)*height
);
444 row_p
= (png_bytep
*) malloc(sizeof(png_bytep
)*height
);
446 for (i
= 0; i
< height
; i
++) {
447 if (StandardOrientation
)
448 row_p
[height
- 1 - i
] = &data
[png_get_rowbytes(png
, info
)*i
];
450 row_p
[i
] = &data
[png_get_rowbytes(png
, info
)*i
];
453 png_read_image(png
, row_p
);
456 rw
= SafeSize(width
), rh
= SafeSize(height
);
458 if (rw
!= width
|| rh
!= height
) {
459 const int channels
= png_get_rowbytes(png
, info
)/width
;
461 data2
= (png_bytep
) malloc(rw
*rh
*channels
);
463 /* Doesn't work on certain sizes */
464 /* if (gluScaleImage(glformat, width, height, GL_UNSIGNED_BYTE, data, rw, rh, GL_UNSIGNED_BYTE, data2) != 0)
467 Resize(channels
, data
, width
, height
, data2
, rw
, rh
);
469 width
= rw
, height
= rh
;
475 glGetIntegerv(GL_PACK_ALIGNMENT
, &pack
);
476 glGetIntegerv(GL_UNPACK_ALIGNMENT
, &unpack
);
477 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
478 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
480 #ifdef SUPPORTS_PALETTE_EXT
481 if (PalettedTextures
&& mipmap
>= 0 && trans
== PNG_SOLID
&& color
== PNG_COLOR_TYPE_PALETTE
) {
486 if (pinfo
!= NULL
) pinfo
->Alpha
= 0;
487 png_get_PLTE(png
, info
, &pal
, &cols
);
490 case 1<<1: intf
= GL_COLOR_INDEX1_EXT
; break;
491 case 1<<2: intf
= GL_COLOR_INDEX2_EXT
; break;
492 case 1<<4: intf
= GL_COLOR_INDEX4_EXT
; break;
493 case 1<<8: intf
= GL_COLOR_INDEX8_EXT
; break;
494 case 1<<12: intf
= GL_COLOR_INDEX12_EXT
; break;
495 case 1<<16: intf
= GL_COLOR_INDEX16_EXT
; break;
497 /*printf("Warning: Colour depth %i not recognised\n", cols);*/
500 glColorTableEXT(GL_TEXTURE_2D
, GL_RGB8
, cols
, GL_RGB
, GL_UNSIGNED_BYTE
, pal
);
501 glTexImage2D(GL_TEXTURE_2D
, mipmap
, intf
, width
, height
, 0, GL_COLOR_INDEX
, GL_UNSIGNED_BYTE
, data
);
505 if (trans
== PNG_SOLID
|| trans
== PNG_ALPHA
|| color
== PNG_COLOR_TYPE_RGB_ALPHA
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
) {
510 case PNG_COLOR_TYPE_GRAY
:
511 case PNG_COLOR_TYPE_RGB
:
512 case PNG_COLOR_TYPE_PALETTE
:
515 if (pinfo
!= NULL
) pinfo
->Alpha
= 0;
518 case PNG_COLOR_TYPE_GRAY_ALPHA
:
519 case PNG_COLOR_TYPE_RGB_ALPHA
:
522 if (pinfo
!= NULL
) pinfo
->Alpha
= 8;
526 /*puts("glformat not set");*/
530 if (mipmap
== PNG_BUILDMIPMAPS
)
531 Build2DMipmaps(glcomponent
, width
, height
, glformat
, data
, 1);
532 else if (mipmap
== PNG_SIMPLEMIPMAPS
)
533 Build2DMipmaps(glcomponent
, width
, height
, glformat
, data
, 0);
535 glTexImage2D(GL_TEXTURE_2D
, mipmap
, glcomponent
, width
, height
, 0, glformat
, GL_UNSIGNED_BYTE
, data
);
538 png_bytep p
, endp
, q
;
541 p
= data
, endp
= p
+width
*height
*3;
542 q
= data2
= (png_bytep
) malloc(sizeof(png_byte
)*width
*height
*4);
544 if (pinfo
!= NULL
) pinfo
->Alpha
= 8;
549 g = *p++; /*green*/ \
550 b = *p++; /*blue */ \
564 ALPHA
= AlphaCallback((unsigned char) r
, (unsigned char) g
, (unsigned char) b
);
570 if (r
== StencilRed
&& g
== StencilGreen
&& b
== StencilBlue
)
580 if (a
> 255) ALPHA
= 255; else ALPHA
= a
;
587 if (a
> 255*2) ALPHA
= 255; else ALPHA
= a
/2;
600 if (a
> 255) ALPHA
= 255; else ALPHA
= a
;
607 if (a
> 255*2) ALPHA
= 255; else ALPHA
= a
/2;
614 if (a
> 255*3) ALPHA
= 255; else ALPHA
= a
/3;
621 if (a
> 255*255) ALPHA
= 255; else ALPHA
= (int) sqrt(a
);
630 if (mipmap
== PNG_BUILDMIPMAPS
)
631 Build2DMipmaps(4, width
, height
, GL_RGBA
, data2
, 1);
632 else if (mipmap
== PNG_SIMPLEMIPMAPS
)
633 Build2DMipmaps(4, width
, height
, GL_RGBA
, data2
, 0);
635 glTexImage2D(GL_TEXTURE_2D
, mipmap
, 4, width
, height
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, data2
);
640 glPixelStorei(GL_PACK_ALIGNMENT
, pack
);
641 glPixelStorei(GL_UNPACK_ALIGNMENT
, unpack
);
644 png_read_end(png
, endinfo
);
645 png_destroy_read_struct(&png
, &info
, &endinfo
);
652 static unsigned int SetParams(int wrapst
, int magfilter
, int minfilter
) {
655 glGenTextures(1, &id
);
656 glBindTexture(GL_TEXTURE_2D
, id
);
658 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, wrapst
);
659 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, wrapst
);
661 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, magfilter
);
662 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, minfilter
);
667 unsigned int APIENTRY
pngBind(const char *filename
, int mipmap
, int trans
, pngInfo
*info
, int wrapst
, int minfilter
, int magfilter
) {
668 unsigned int id
= SetParams(wrapst
, magfilter
, minfilter
);
670 if (id
!= 0 && pngLoad(filename
, mipmap
, trans
, info
))
675 unsigned int APIENTRY
pngBindF(FILE *file
, int mipmap
, int trans
, pngInfo
*info
, int wrapst
, int minfilter
, int magfilter
) {
676 unsigned int id
= SetParams(wrapst
, magfilter
, minfilter
);
678 if (id
!= 0 && pngLoadF(file
, mipmap
, trans
, info
))
683 void APIENTRY
pngSetStencil(unsigned char red
, unsigned char green
, unsigned char blue
) {
684 StencilRed
= red
, StencilGreen
= green
, StencilBlue
= blue
;
687 void APIENTRY
pngSetAlphaCallback(unsigned char (*callback
)(unsigned char red
, unsigned char green
, unsigned char blue
)) {
688 if (callback
== NULL
)
689 AlphaCallback
= DefaultAlphaCallback
;
691 AlphaCallback
= callback
;
694 void APIENTRY
pngSetViewingGamma(double viewingGamma
) {
695 if(viewingGamma
> 0) {
697 screenGamma
= 2.2/viewingGamma
;
705 void APIENTRY
pngSetStandardOrientation(int standardorientation
) {
706 StandardOrientation
= standardorientation
;