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 */
28 #define GL_GLEXT_PROTOTYPES
37 /* Used to decide if GL/gl.h supports the paletted extension */
38 #ifdef GL_COLOR_INDEX1_EXT
39 #define SUPPORTS_PALETTE_EXT
42 static unsigned char DefaultAlphaCallback(unsigned char red
, unsigned char green
, unsigned char blue
) {
46 static unsigned char StencilRed
= 0, StencilGreen
= 0, StencilBlue
= 0;
47 static unsigned char (*AlphaCallback
)(unsigned char red
, unsigned char green
, unsigned char blue
) = DefaultAlphaCallback
;
48 static int StandardOrientation
= 0;
50 #ifdef SUPPORTS_PALETTE_EXT
52 static PFNGLCOLORTABLEEXTPROC glColorTableEXT
= NULL
;
56 static int PalettedTextures
= -1;
57 static GLint MaxTextureSize
= 0;
59 /* screenGamma = displayGamma/viewingGamma
60 * displayGamma = CRT has gamma of ~2.2
61 * viewingGamma depends on platform. PC is 1.0, Mac is 1.45, SGI defaults
62 * to 1.7, but this can be checked and changed w/ /usr/sbin/gamma command.
63 * If the environment variable VIEWING_GAMMA is set, adjust gamma per this value.
66 static double screenGamma
= 2.2 / 1.45;
68 static double screenGamma
= 2.2 / 1.7;
69 #else /* PC/default */
70 static double screenGamma
= 2.2 / 1.0;
73 static char gammaExplicit
= 0; /*if */
75 static void checkForGammaEnv()
78 char *gammaEnv
= getenv("VIEWING_GAMMA");
80 if(gammaEnv
&& !gammaExplicit
)
82 sscanf(gammaEnv
, "%lf", &viewingGamma
);
83 screenGamma
= 2.2/viewingGamma
;
87 /* Returns a safe texture size to use (ie a power of 2), based on the current texture size "i" */
88 static int SafeSize(int i
) {
91 if (i
> MaxTextureSize
) return MaxTextureSize
;
93 for (p
= 0; p
< 24; p
++)
97 return MaxTextureSize
;
100 /* Resize the texture since gluScaleImage doesn't work on everything */
101 static void Resize(int components
, const png_bytep d1
, int w1
, int h1
, png_bytep d2
, int w2
, int h2
) {
102 const float sx
= (float) w1
/w2
, sy
= (float) h1
/h2
;
106 for (y
= 0; y
< h2
; y
++) {
107 yy
= (int) (y
*sy
)*w1
;
109 for (x
= 0; x
< w2
; x
++) {
111 d
= d1
+ (yy
+xx
)*components
;
113 for (c
= 0; c
< components
; c
++)
120 static int ExtSupported(const char *x
) {
121 static const GLubyte
*ext
= NULL
;
123 int xlen
= strlen(x
);
125 if (ext
== NULL
) ext
= glGetString(GL_EXTENSIONS
);
127 c
= (const char*)ext
;
130 if (strcmp(c
, x
) == 0 && (c
[xlen
] == '\0' || c
[xlen
] == ' ')) return 1;
138 #define GET(o) ((int)*(data + (o)))
140 static int HalfSize(GLint components
, GLint width
, GLint height
, const unsigned char *data
, unsigned char *d
, int filter
) {
142 int line
= width
*components
;
144 if (width
> 1 && height
> 1) {
146 for (y
= 0; y
< height
; y
+= 2) {
147 for (x
= 0; x
< width
; x
+= 2) {
148 for (c
= 0; c
< components
; c
++) {
149 *d
++ = (GET(0)+GET(components
)+GET(line
)+GET(line
+components
)) / 4;
157 for (y
= 0; y
< height
; y
+= 2) {
158 for (x
= 0; x
< width
; x
+= 2) {
159 for (c
= 0; c
< components
; c
++) {
168 else if (width
> 1 && height
== 1) {
170 for (y
= 0; y
< height
; y
+= 1) {
171 for (x
= 0; x
< width
; x
+= 2) {
172 for (c
= 0; c
< components
; c
++) {
173 *d
++ = (GET(0)+GET(components
)) / 2;
180 for (y
= 0; y
< height
; y
+= 1) {
181 for (x
= 0; x
< width
; x
+= 2) {
182 for (c
= 0; c
< components
; c
++) {
190 else if (width
== 1 && height
> 1) {
192 for (y
= 0; y
< height
; y
+= 2) {
193 for (x
= 0; x
< width
; x
+= 1) {
194 for (c
= 0; c
< components
; c
++) {
195 *d
++ = (GET(0)+GET(line
)) / 2;
202 for (y
= 0; y
< height
; y
+= 2) {
203 for (x
= 0; x
< width
; x
+= 1) {
204 for (c
= 0; c
< components
; c
++) {
221 /* Replacement for gluBuild2DMipmaps so GLU isn't needed */
222 static void Build2DMipmaps(GLint components
, GLint width
, GLint height
, GLenum format
, const unsigned char *data
, int filter
) {
224 unsigned char *d
= (unsigned char *) malloc((width
/2)*(height
/2)*components
+4);
225 const unsigned char *last
= data
;
227 glTexImage2D(GL_TEXTURE_2D
, level
, components
, width
, height
, 0, format
, GL_UNSIGNED_BYTE
, data
);
230 while (HalfSize(components
, width
, height
, last
, d
, filter
)) {
231 if (width
> 1) width
/= 2;
232 if (height
> 1) height
/= 2;
234 glTexImage2D(GL_TEXTURE_2D
, level
, components
, width
, height
, 0, format
, GL_UNSIGNED_BYTE
, d
);
242 int APIENTRY
pngLoadRaw(const char *filename
, pngRawInfo
*pinfo
) {
244 FILE *fp
= fopen(filename
, "rb");
245 if (fp
== NULL
) return 0;
247 result
= pngLoadRawF(fp
, pinfo
);
249 if (fclose(fp
) != 0) {
252 free(pinfo
->Palette
);
260 int APIENTRY
pngLoadRawF(FILE *fp
, pngRawInfo
*pinfo
) {
261 unsigned char header
[8];
269 png_uint_32 width
, height
;
274 if (pinfo
== NULL
) return 0;
276 fread(header
, 1, 8, fp
);
277 if (!png_check_sig(header
, 8)) return 0;
279 png
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
280 info
= png_create_info_struct(png
);
281 endinfo
= png_create_info_struct(png
);
283 // DH: added following lines
284 if (setjmp(png
->jmpbuf
))
286 png_destroy_read_struct(&png
, &info
, &endinfo
);
291 png_init_io(png
, fp
);
292 png_set_sig_bytes(png
, 8);
293 png_read_info(png
, info
);
294 png_get_IHDR(png
, info
, &width
, &height
, &depth
, &color
, NULL
, NULL
, NULL
);
296 pinfo
->Width
= width
;
297 pinfo
->Height
= height
;
298 pinfo
->Depth
= depth
;
302 if (png_get_gAMA(png
, info
, &fileGamma
))
303 png_set_gamma(png
, screenGamma
, fileGamma
);
305 png_set_gamma(png
, screenGamma
, 1.0/2.2);
307 png_read_update_info(png
, info
);
309 data
= (png_bytep
) malloc(png_get_rowbytes(png
, info
)*height
);
310 row_p
= (png_bytep
*) malloc(sizeof(png_bytep
)*height
);
312 for (i
= 0; i
< height
; i
++) {
313 if (StandardOrientation
)
314 row_p
[height
- 1 - i
] = &data
[png_get_rowbytes(png
, info
)*i
];
316 row_p
[i
] = &data
[png_get_rowbytes(png
, info
)*i
];
319 png_read_image(png
, row_p
);
322 if (color
== PNG_COLOR_TYPE_PALETTE
) {
324 png_get_PLTE(png
, info
, (png_colorp
*) &pinfo
->Palette
, &cols
);
327 pinfo
->Palette
= NULL
;
330 if (color
&PNG_COLOR_MASK_ALPHA
) {
331 if (color
&PNG_COLOR_MASK_PALETTE
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
)
332 pinfo
->Components
= 2;
334 pinfo
->Components
= 4;
338 if (color
&PNG_COLOR_MASK_PALETTE
|| color
== PNG_COLOR_TYPE_GRAY
)
339 pinfo
->Components
= 1;
341 pinfo
->Components
= 3;
347 png_read_end(png
, endinfo
);
348 png_destroy_read_struct(&png
, &info
, &endinfo
);
353 int APIENTRY
pngLoad(const char *filename
, int mipmap
, int trans
, pngInfo
*pinfo
) {
355 FILE *fp
= fopen(filename
, "rb");
356 if (fp
== NULL
) return 0;
358 result
= pngLoadF(fp
, mipmap
, trans
, pinfo
);
360 if (fclose(fp
) != 0) return 0;
365 int APIENTRY
pngLoadF(FILE *fp
, int mipmap
, int trans
, pngInfo
*pinfo
) {
367 unsigned char header
[8];
371 png_bytep data
, data2
;
375 png_uint_32 width
, height
, rw
, rh
;
380 fread(header
, 1, 8, fp
);
381 if (!png_check_sig(header
, 8)) return 0;
383 png
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
384 info
= png_create_info_struct(png
);
385 endinfo
= png_create_info_struct(png
);
387 // DH: added following lines
388 if (setjmp(png
->jmpbuf
))
390 png_destroy_read_struct(&png
, &info
, &endinfo
);
395 png_init_io(png
, fp
);
396 png_set_sig_bytes(png
, 8);
397 png_read_info(png
, info
);
398 png_get_IHDR(png
, info
, &width
, &height
, &depth
, &color
, NULL
, NULL
, NULL
);
401 pinfo
->Width
= width
;
402 pinfo
->Height
= height
;
403 pinfo
->Depth
= depth
;
406 if (MaxTextureSize
== 0)
407 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, &MaxTextureSize
);
409 #ifdef SUPPORTS_PALETTE_EXT
411 if (PalettedTextures
== -1)
412 PalettedTextures
= ExtSupported("GL_EXT_paletted_texture") && (strstr((const char *) glGetString(GL_VERSION
), "1.1.0 3Dfx Beta") == NULL
);
414 if (PalettedTextures
) {
415 if (glColorTableEXT
== NULL
) {
416 glColorTableEXT
= (PFNGLCOLORTABLEEXTPROC
) wglGetProcAddress("glColorTableEXT");
417 if (glColorTableEXT
== NULL
)
418 PalettedTextures
= 0;
424 if (PalettedTextures
== -1)
425 PalettedTextures
= 0;
427 if (color
== PNG_COLOR_TYPE_GRAY
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
)
428 png_set_gray_to_rgb(png
);
430 if (color
&PNG_COLOR_MASK_ALPHA
&& trans
!= PNG_ALPHA
) {
431 png_set_strip_alpha(png
);
432 color
&= ~PNG_COLOR_MASK_ALPHA
;
435 if (!(PalettedTextures
&& mipmap
>= 0 && trans
== PNG_SOLID
))
436 if (color
== PNG_COLOR_TYPE_PALETTE
)
441 if (png_get_gAMA(png
, info
, &fileGamma
))
442 png_set_gamma(png
, screenGamma
, fileGamma
);
444 png_set_gamma(png
, screenGamma
, 1.0/2.2);
446 png_read_update_info(png
, info
);
448 data
= (png_bytep
) malloc(png_get_rowbytes(png
, info
)*height
);
449 row_p
= (png_bytep
*) malloc(sizeof(png_bytep
)*height
);
451 for (i
= 0; i
< height
; i
++) {
452 if (StandardOrientation
)
453 row_p
[height
- 1 - i
] = &data
[png_get_rowbytes(png
, info
)*i
];
455 row_p
[i
] = &data
[png_get_rowbytes(png
, info
)*i
];
458 png_read_image(png
, row_p
);
461 rw
= SafeSize(width
), rh
= SafeSize(height
);
463 if (rw
!= width
|| rh
!= height
) {
464 const int channels
= png_get_rowbytes(png
, info
)/width
;
466 data2
= (png_bytep
) malloc(rw
*rh
*channels
);
468 /* Doesn't work on certain sizes */
469 /* if (gluScaleImage(glformat, width, height, GL_UNSIGNED_BYTE, data, rw, rh, GL_UNSIGNED_BYTE, data2) != 0)
472 Resize(channels
, data
, width
, height
, data2
, rw
, rh
);
474 width
= rw
, height
= rh
;
480 glGetIntegerv(GL_PACK_ALIGNMENT
, &pack
);
481 glGetIntegerv(GL_UNPACK_ALIGNMENT
, &unpack
);
482 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
483 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
485 #ifdef SUPPORTS_PALETTE_EXT
486 if (PalettedTextures
&& mipmap
>= 0 && trans
== PNG_SOLID
&& color
== PNG_COLOR_TYPE_PALETTE
) {
491 if (pinfo
!= NULL
) pinfo
->Alpha
= 0;
492 png_get_PLTE(png
, info
, &pal
, &cols
);
495 case 1<<1: intf
= GL_COLOR_INDEX1_EXT
; break;
496 case 1<<2: intf
= GL_COLOR_INDEX2_EXT
; break;
497 case 1<<4: intf
= GL_COLOR_INDEX4_EXT
; break;
498 case 1<<8: intf
= GL_COLOR_INDEX8_EXT
; break;
499 case 1<<12: intf
= GL_COLOR_INDEX12_EXT
; break;
500 case 1<<16: intf
= GL_COLOR_INDEX16_EXT
; break;
502 /*printf("Warning: Colour depth %i not recognised\n", cols);*/
505 glColorTableEXT(GL_TEXTURE_2D
, GL_RGB8
, cols
, GL_RGB
, GL_UNSIGNED_BYTE
, pal
);
506 glTexImage2D(GL_TEXTURE_2D
, mipmap
, intf
, width
, height
, 0, GL_COLOR_INDEX
, GL_UNSIGNED_BYTE
, data
);
510 if (trans
== PNG_SOLID
|| trans
== PNG_ALPHA
|| color
== PNG_COLOR_TYPE_RGB_ALPHA
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
) {
515 case PNG_COLOR_TYPE_GRAY
:
516 case PNG_COLOR_TYPE_RGB
:
517 case PNG_COLOR_TYPE_PALETTE
:
520 if (pinfo
!= NULL
) pinfo
->Alpha
= 0;
523 case PNG_COLOR_TYPE_GRAY_ALPHA
:
524 case PNG_COLOR_TYPE_RGB_ALPHA
:
527 if (pinfo
!= NULL
) pinfo
->Alpha
= 8;
531 /*puts("glformat not set");*/
535 if (mipmap
== PNG_BUILDMIPMAPS
)
536 Build2DMipmaps(glcomponent
, width
, height
, glformat
, data
, 1);
537 else if (mipmap
== PNG_SIMPLEMIPMAPS
)
538 Build2DMipmaps(glcomponent
, width
, height
, glformat
, data
, 0);
540 glTexImage2D(GL_TEXTURE_2D
, mipmap
, glcomponent
, width
, height
, 0, glformat
, GL_UNSIGNED_BYTE
, data
);
543 png_bytep p
, endp
, q
;
546 p
= data
, endp
= p
+width
*height
*3;
547 q
= data2
= (png_bytep
) malloc(sizeof(png_byte
)*width
*height
*4);
549 if (pinfo
!= NULL
) pinfo
->Alpha
= 8;
554 g = *p++; /*green*/ \
555 b = *p++; /*blue */ \
569 ALPHA
= AlphaCallback((unsigned char) r
, (unsigned char) g
, (unsigned char) b
);
575 if (r
== StencilRed
&& g
== StencilGreen
&& b
== StencilBlue
)
585 if (a
> 255) ALPHA
= 255; else ALPHA
= a
;
592 if (a
> 255*2) ALPHA
= 255; else ALPHA
= a
/2;
605 if (a
> 255) ALPHA
= 255; else ALPHA
= a
;
612 if (a
> 255*2) ALPHA
= 255; else ALPHA
= a
/2;
619 if (a
> 255*3) ALPHA
= 255; else ALPHA
= a
/3;
626 if (a
> 255*255) ALPHA
= 255; else ALPHA
= (int) sqrt(a
);
635 if (mipmap
== PNG_BUILDMIPMAPS
)
636 Build2DMipmaps(4, width
, height
, GL_RGBA
, data2
, 1);
637 else if (mipmap
== PNG_SIMPLEMIPMAPS
)
638 Build2DMipmaps(4, width
, height
, GL_RGBA
, data2
, 0);
640 glTexImage2D(GL_TEXTURE_2D
, mipmap
, 4, width
, height
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, data2
);
645 glPixelStorei(GL_PACK_ALIGNMENT
, pack
);
646 glPixelStorei(GL_UNPACK_ALIGNMENT
, unpack
);
649 png_read_end(png
, endinfo
);
650 png_destroy_read_struct(&png
, &info
, &endinfo
);
657 static unsigned int SetParams(int wrapst
, int magfilter
, int minfilter
) {
660 glGenTextures(1, &id
);
661 glBindTexture(GL_TEXTURE_2D
, id
);
663 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, wrapst
);
664 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, wrapst
);
666 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, magfilter
);
667 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, minfilter
);
672 unsigned int APIENTRY
pngBind(const char *filename
, int mipmap
, int trans
, pngInfo
*info
, int wrapst
, int minfilter
, int magfilter
) {
673 unsigned int id
= SetParams(wrapst
, magfilter
, minfilter
);
675 if (id
!= 0 && pngLoad(filename
, mipmap
, trans
, info
))
680 unsigned int APIENTRY
pngBindF(FILE *file
, int mipmap
, int trans
, pngInfo
*info
, int wrapst
, int minfilter
, int magfilter
) {
681 unsigned int id
= SetParams(wrapst
, magfilter
, minfilter
);
683 if (id
!= 0 && pngLoadF(file
, mipmap
, trans
, info
))
688 void APIENTRY
pngSetStencil(unsigned char red
, unsigned char green
, unsigned char blue
) {
689 StencilRed
= red
, StencilGreen
= green
, StencilBlue
= blue
;
692 void APIENTRY
pngSetAlphaCallback(unsigned char (*callback
)(unsigned char red
, unsigned char green
, unsigned char blue
)) {
693 if (callback
== NULL
)
694 AlphaCallback
= DefaultAlphaCallback
;
696 AlphaCallback
= callback
;
699 void APIENTRY
pngSetViewingGamma(double viewingGamma
) {
700 if(viewingGamma
> 0) {
702 screenGamma
= 2.2/viewingGamma
;
710 void APIENTRY
pngSetStandardOrientation(int standardorientation
) {
711 StandardOrientation
= standardorientation
;