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
++)
117 static int ExtSupported(const char *x
) {
118 static const GLubyte
*ext
= NULL
;
120 int xlen
= strlen(x
);
122 if (ext
== NULL
) ext
= glGetString(GL_EXTENSIONS
);
124 c
= (const char*)ext
;
127 if (strcmp(c
, x
) == 0 && (c
[xlen
] == '\0' || c
[xlen
] == ' ')) return 1;
135 #define GET(o) ((int)*(data + (o)))
137 static int HalfSize(GLint components
, GLint width
, GLint height
, const unsigned char *data
, unsigned char *d
, int filter
) {
139 int line
= width
*components
;
141 if (width
> 1 && height
> 1) {
143 for (y
= 0; y
< height
; y
+= 2) {
144 for (x
= 0; x
< width
; x
+= 2) {
145 for (c
= 0; c
< components
; c
++) {
146 *d
++ = (GET(0)+GET(components
)+GET(line
)+GET(line
+components
)) / 4;
154 for (y
= 0; y
< height
; y
+= 2) {
155 for (x
= 0; x
< width
; x
+= 2) {
156 for (c
= 0; c
< components
; c
++) {
165 else if (width
> 1 && height
== 1) {
167 for (y
= 0; y
< height
; y
+= 1) {
168 for (x
= 0; x
< width
; x
+= 2) {
169 for (c
= 0; c
< components
; c
++) {
170 *d
++ = (GET(0)+GET(components
)) / 2;
177 for (y
= 0; y
< height
; y
+= 1) {
178 for (x
= 0; x
< width
; x
+= 2) {
179 for (c
= 0; c
< components
; c
++) {
187 else if (width
== 1 && height
> 1) {
189 for (y
= 0; y
< height
; y
+= 2) {
190 for (x
= 0; x
< width
; x
+= 1) {
191 for (c
= 0; c
< components
; c
++) {
192 *d
++ = (GET(0)+GET(line
)) / 2;
199 for (y
= 0; y
< height
; y
+= 2) {
200 for (x
= 0; x
< width
; x
+= 1) {
201 for (c
= 0; c
< components
; c
++) {
218 /* Replacement for gluBuild2DMipmaps so GLU isn't needed */
219 static void Build2DMipmaps(GLint components
, GLint width
, GLint height
, GLenum format
, const unsigned char *data
, int filter
) {
221 unsigned char *d
= (unsigned char *) malloc((width
/2)*(height
/2)*components
+4);
222 const unsigned char *last
= data
;
224 glTexImage2D(GL_TEXTURE_2D
, level
, components
, width
, height
, 0, format
, GL_UNSIGNED_BYTE
, data
);
227 while (HalfSize(components
, width
, height
, last
, d
, filter
)) {
228 if (width
> 1) width
/= 2;
229 if (height
> 1) height
/= 2;
231 glTexImage2D(GL_TEXTURE_2D
, level
, components
, width
, height
, 0, format
, GL_UNSIGNED_BYTE
, d
);
239 int APIENTRY
pngLoadRaw(const char *filename
, pngRawInfo
*pinfo
) {
241 FILE *fp
= fopen(filename
, "rb");
242 if (fp
== NULL
) return 0;
244 result
= pngLoadRawF(fp
, pinfo
);
246 if (fclose(fp
) != 0) {
249 free(pinfo
->Palette
);
257 int APIENTRY
pngLoadRawF(FILE *fp
, pngRawInfo
*pinfo
) {
258 unsigned char header
[8];
266 png_uint_32 width
, height
;
271 if (pinfo
== NULL
) return 0;
273 fread(header
, 1, 8, fp
);
274 if (!png_check_sig(header
, 8)) return 0;
276 png
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
277 info
= png_create_info_struct(png
);
278 endinfo
= png_create_info_struct(png
);
280 // DH: added following lines
281 if (setjmp(png
->jmpbuf
))
283 png_destroy_read_struct(&png
, &info
, &endinfo
);
288 png_init_io(png
, fp
);
289 png_set_sig_bytes(png
, 8);
290 png_read_info(png
, info
);
291 png_get_IHDR(png
, info
, &width
, &height
, &depth
, &color
, NULL
, NULL
, NULL
);
293 pinfo
->Width
= width
;
294 pinfo
->Height
= height
;
295 pinfo
->Depth
= depth
;
299 if (png_get_gAMA(png
, info
, &fileGamma
))
300 png_set_gamma(png
, screenGamma
, fileGamma
);
302 png_set_gamma(png
, screenGamma
, 1.0/2.2);
304 png_read_update_info(png
, info
);
306 data
= (png_bytep
) malloc(png_get_rowbytes(png
, info
)*height
);
307 row_p
= (png_bytep
*) malloc(sizeof(png_bytep
)*height
);
309 for (i
= 0; i
< height
; i
++) {
310 if (StandardOrientation
)
311 row_p
[height
- 1 - i
] = &data
[png_get_rowbytes(png
, info
)*i
];
313 row_p
[i
] = &data
[png_get_rowbytes(png
, info
)*i
];
316 png_read_image(png
, row_p
);
319 if (color
== PNG_COLOR_TYPE_PALETTE
) {
321 png_get_PLTE(png
, info
, (png_colorp
*) &pinfo
->Palette
, &cols
);
324 pinfo
->Palette
= NULL
;
327 if (color
&PNG_COLOR_MASK_ALPHA
) {
328 if (color
&PNG_COLOR_MASK_PALETTE
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
)
329 pinfo
->Components
= 2;
331 pinfo
->Components
= 4;
335 if (color
&PNG_COLOR_MASK_PALETTE
|| color
== PNG_COLOR_TYPE_GRAY
)
336 pinfo
->Components
= 1;
338 pinfo
->Components
= 3;
344 png_read_end(png
, endinfo
);
345 png_destroy_read_struct(&png
, &info
, &endinfo
);
350 int APIENTRY
pngLoad(const char *filename
, int mipmap
, int trans
, pngInfo
*pinfo
) {
352 FILE *fp
= fopen(filename
, "rb");
353 if (fp
== NULL
) return 0;
355 result
= pngLoadF(fp
, mipmap
, trans
, pinfo
);
357 if (fclose(fp
) != 0) return 0;
362 int APIENTRY
pngLoadF(FILE *fp
, int mipmap
, int trans
, pngInfo
*pinfo
) {
364 unsigned char header
[8];
368 png_bytep data
, data2
;
372 png_uint_32 width
, height
, rw
, rh
;
377 fread(header
, 1, 8, fp
);
378 if (!png_check_sig(header
, 8)) return 0;
380 png
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
381 info
= png_create_info_struct(png
);
382 endinfo
= png_create_info_struct(png
);
384 // DH: added following lines
385 if (setjmp(png
->jmpbuf
))
387 png_destroy_read_struct(&png
, &info
, &endinfo
);
392 png_init_io(png
, fp
);
393 png_set_sig_bytes(png
, 8);
394 png_read_info(png
, info
);
395 png_get_IHDR(png
, info
, &width
, &height
, &depth
, &color
, NULL
, NULL
, NULL
);
398 pinfo
->Width
= width
;
399 pinfo
->Height
= height
;
400 pinfo
->Depth
= depth
;
403 if (MaxTextureSize
== 0)
404 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, &MaxTextureSize
);
406 #ifdef SUPPORTS_PALETTE_EXT
408 if (PalettedTextures
== -1)
409 PalettedTextures
= ExtSupported("GL_EXT_paletted_texture") && (strstr((const char *) glGetString(GL_VERSION
), "1.1.0 3Dfx Beta") == NULL
);
411 if (PalettedTextures
) {
412 if (glColorTableEXT
== NULL
) {
413 glColorTableEXT
= (PFNGLCOLORTABLEEXTPROC
) wglGetProcAddress("glColorTableEXT");
414 if (glColorTableEXT
== NULL
)
415 PalettedTextures
= 0;
421 if (PalettedTextures
== -1)
422 PalettedTextures
= 0;
424 if (color
== PNG_COLOR_TYPE_GRAY
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
)
425 png_set_gray_to_rgb(png
);
427 if (color
&PNG_COLOR_MASK_ALPHA
&& trans
!= PNG_ALPHA
) {
428 png_set_strip_alpha(png
);
429 color
&= ~PNG_COLOR_MASK_ALPHA
;
432 if (!(PalettedTextures
&& mipmap
>= 0 && trans
== PNG_SOLID
))
433 if (color
== PNG_COLOR_TYPE_PALETTE
)
438 if (png_get_gAMA(png
, info
, &fileGamma
))
439 png_set_gamma(png
, screenGamma
, fileGamma
);
441 png_set_gamma(png
, screenGamma
, 1.0/2.2);
443 png_read_update_info(png
, info
);
445 data
= (png_bytep
) malloc(png_get_rowbytes(png
, info
)*height
);
446 row_p
= (png_bytep
*) malloc(sizeof(png_bytep
)*height
);
448 for (i
= 0; i
< height
; i
++) {
449 if (StandardOrientation
)
450 row_p
[height
- 1 - i
] = &data
[png_get_rowbytes(png
, info
)*i
];
452 row_p
[i
] = &data
[png_get_rowbytes(png
, info
)*i
];
455 png_read_image(png
, row_p
);
458 rw
= SafeSize(width
), rh
= SafeSize(height
);
460 if (rw
!= width
|| rh
!= height
) {
461 const int channels
= png_get_rowbytes(png
, info
)/width
;
463 data2
= (png_bytep
) malloc(rw
*rh
*channels
);
465 /* Doesn't work on certain sizes */
466 /* if (gluScaleImage(glformat, width, height, GL_UNSIGNED_BYTE, data, rw, rh, GL_UNSIGNED_BYTE, data2) != 0)
469 Resize(channels
, data
, width
, height
, data2
, rw
, rh
);
471 width
= rw
, height
= rh
;
477 glGetIntegerv(GL_PACK_ALIGNMENT
, &pack
);
478 glGetIntegerv(GL_UNPACK_ALIGNMENT
, &unpack
);
479 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
480 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
482 #ifdef SUPPORTS_PALETTE_EXT
483 if (PalettedTextures
&& mipmap
>= 0 && trans
== PNG_SOLID
&& color
== PNG_COLOR_TYPE_PALETTE
) {
488 if (pinfo
!= NULL
) pinfo
->Alpha
= 0;
489 png_get_PLTE(png
, info
, &pal
, &cols
);
492 case 1<<1: intf
= GL_COLOR_INDEX1_EXT
; break;
493 case 1<<2: intf
= GL_COLOR_INDEX2_EXT
; break;
494 case 1<<4: intf
= GL_COLOR_INDEX4_EXT
; break;
495 case 1<<8: intf
= GL_COLOR_INDEX8_EXT
; break;
496 case 1<<12: intf
= GL_COLOR_INDEX12_EXT
; break;
497 case 1<<16: intf
= GL_COLOR_INDEX16_EXT
; break;
499 /*printf("Warning: Colour depth %i not recognised\n", cols);*/
502 glColorTableEXT(GL_TEXTURE_2D
, GL_RGB8
, cols
, GL_RGB
, GL_UNSIGNED_BYTE
, pal
);
503 glTexImage2D(GL_TEXTURE_2D
, mipmap
, intf
, width
, height
, 0, GL_COLOR_INDEX
, GL_UNSIGNED_BYTE
, data
);
507 if (trans
== PNG_SOLID
|| trans
== PNG_ALPHA
|| color
== PNG_COLOR_TYPE_RGB_ALPHA
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
) {
512 case PNG_COLOR_TYPE_GRAY
:
513 case PNG_COLOR_TYPE_RGB
:
514 case PNG_COLOR_TYPE_PALETTE
:
517 if (pinfo
!= NULL
) pinfo
->Alpha
= 0;
520 case PNG_COLOR_TYPE_GRAY_ALPHA
:
521 case PNG_COLOR_TYPE_RGB_ALPHA
:
524 if (pinfo
!= NULL
) pinfo
->Alpha
= 8;
528 /*puts("glformat not set");*/
532 if (mipmap
== PNG_BUILDMIPMAPS
)
533 Build2DMipmaps(glcomponent
, width
, height
, glformat
, data
, 1);
534 else if (mipmap
== PNG_SIMPLEMIPMAPS
)
535 Build2DMipmaps(glcomponent
, width
, height
, glformat
, data
, 0);
537 glTexImage2D(GL_TEXTURE_2D
, mipmap
, glcomponent
, width
, height
, 0, glformat
, GL_UNSIGNED_BYTE
, data
);
540 png_bytep p
, endp
, q
;
543 p
= data
, endp
= p
+width
*height
*3;
544 q
= data2
= (png_bytep
) malloc(sizeof(png_byte
)*width
*height
*4);
546 if (pinfo
!= NULL
) pinfo
->Alpha
= 8;
551 g = *p++; /*green*/ \
552 b = *p++; /*blue */ \
566 ALPHA
= AlphaCallback((unsigned char) r
, (unsigned char) g
, (unsigned char) b
);
572 if (r
== StencilRed
&& g
== StencilGreen
&& b
== StencilBlue
)
582 if (a
> 255) ALPHA
= 255; else ALPHA
= a
;
589 if (a
> 255*2) ALPHA
= 255; else ALPHA
= a
/2;
602 if (a
> 255) ALPHA
= 255; else ALPHA
= a
;
609 if (a
> 255*2) ALPHA
= 255; else ALPHA
= a
/2;
616 if (a
> 255*3) ALPHA
= 255; else ALPHA
= a
/3;
623 if (a
> 255*255) ALPHA
= 255; else ALPHA
= (int) sqrt(a
);
632 if (mipmap
== PNG_BUILDMIPMAPS
)
633 Build2DMipmaps(4, width
, height
, GL_RGBA
, data2
, 1);
634 else if (mipmap
== PNG_SIMPLEMIPMAPS
)
635 Build2DMipmaps(4, width
, height
, GL_RGBA
, data2
, 0);
637 glTexImage2D(GL_TEXTURE_2D
, mipmap
, 4, width
, height
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, data2
);
642 glPixelStorei(GL_PACK_ALIGNMENT
, pack
);
643 glPixelStorei(GL_UNPACK_ALIGNMENT
, unpack
);
646 png_read_end(png
, endinfo
);
647 png_destroy_read_struct(&png
, &info
, &endinfo
);
654 static unsigned int SetParams(int wrapst
, int magfilter
, int minfilter
) {
657 glGenTextures(1, &id
);
658 glBindTexture(GL_TEXTURE_2D
, id
);
660 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, wrapst
);
661 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, wrapst
);
663 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, magfilter
);
664 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, minfilter
);
669 unsigned int APIENTRY
pngBind(const char *filename
, int mipmap
, int trans
, pngInfo
*info
, int wrapst
, int minfilter
, int magfilter
) {
670 unsigned int id
= SetParams(wrapst
, magfilter
, minfilter
);
672 if (id
!= 0 && pngLoad(filename
, mipmap
, trans
, info
))
677 unsigned int APIENTRY
pngBindF(FILE *file
, int mipmap
, int trans
, pngInfo
*info
, int wrapst
, int minfilter
, int magfilter
) {
678 unsigned int id
= SetParams(wrapst
, magfilter
, minfilter
);
680 if (id
!= 0 && pngLoadF(file
, mipmap
, trans
, info
))
685 void APIENTRY
pngSetStencil(unsigned char red
, unsigned char green
, unsigned char blue
) {
686 StencilRed
= red
, StencilGreen
= green
, StencilBlue
= blue
;
689 void APIENTRY
pngSetAlphaCallback(unsigned char (*callback
)(unsigned char red
, unsigned char green
, unsigned char blue
)) {
690 if (callback
== NULL
)
691 AlphaCallback
= DefaultAlphaCallback
;
693 AlphaCallback
= callback
;
696 void APIENTRY
pngSetViewingGamma(double viewingGamma
) {
697 if(viewingGamma
> 0) {
699 screenGamma
= 2.2/viewingGamma
;
707 void APIENTRY
pngSetStandardOrientation(int standardorientation
) {
708 StandardOrientation
= standardorientation
;