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
39 /* Used to decide if GL/gl.h supports the paletted extension */
40 #ifdef GL_COLOR_INDEX1_EXT
41 #define SUPPORTS_PALETTE_EXT
44 static unsigned char DefaultAlphaCallback(unsigned char red
, unsigned char green
, unsigned char blue
) {
48 static unsigned char StencilRed
= 0, StencilGreen
= 0, StencilBlue
= 0;
49 static unsigned char (*AlphaCallback
)(unsigned char red
, unsigned char green
, unsigned char blue
) = DefaultAlphaCallback
;
50 static int StandardOrientation
= 0;
52 #ifdef SUPPORTS_PALETTE_EXT
54 static PFNGLCOLORTABLEEXTPROC glColorTableEXT
= NULL
;
58 static int PalettedTextures
= -1;
59 static GLint MaxTextureSize
= 0;
61 /* screenGamma = displayGamma/viewingGamma
62 * displayGamma = CRT has gamma of ~2.2
63 * viewingGamma depends on platform. PC is 1.0, Mac is 1.45, SGI defaults
64 * to 1.7, but this can be checked and changed w/ /usr/sbin/gamma command.
65 * If the environment variable VIEWING_GAMMA is set, adjust gamma per this value.
68 static double screenGamma
= 2.2 / 1.45;
70 static double screenGamma
= 2.2 / 1.7;
71 #else /* PC/default */
72 static double screenGamma
= 2.2 / 1.0;
75 static char gammaExplicit
= 0; /*if */
77 static void checkForGammaEnv()
80 char *gammaEnv
= getenv("VIEWING_GAMMA");
82 if(gammaEnv
&& !gammaExplicit
)
84 sscanf(gammaEnv
, "%lf", &viewingGamma
);
85 screenGamma
= 2.2/viewingGamma
;
89 /* Returns a safe texture size to use (ie a power of 2), based on the current texture size "i" */
90 static int SafeSize(int i
) {
93 if (i
> MaxTextureSize
) return MaxTextureSize
;
95 for (p
= 0; p
< 24; p
++)
99 return MaxTextureSize
;
102 /* Resize the texture since gluScaleImage doesn't work on everything */
103 static void Resize(int components
, const png_bytep d1
, int w1
, int h1
, png_bytep d2
, int w2
, int h2
) {
104 const float sx
= (float) w1
/w2
, sy
= (float) h1
/h2
;
108 for (y
= 0; y
< h2
; y
++) {
109 yy
= (int) (y
*sy
)*w1
;
111 for (x
= 0; x
< w2
; x
++) {
113 d
= d1
+ (yy
+xx
)*components
;
115 for (c
= 0; c
< components
; c
++)
121 #ifdef SUPPORTS_PALETTE_EXT
123 static int ExtSupported(const char *x
) {
124 static const GLubyte
*ext
= NULL
;
126 int xlen
= strlen(x
);
128 if (ext
== NULL
) ext
= glGetString(GL_EXTENSIONS
);
130 c
= (const char*)ext
;
133 if (strcmp(c
, x
) == 0 && (c
[xlen
] == '\0' || c
[xlen
] == ' ')) return 1;
142 #define GET(o) ((int)*(data + (o)))
144 static int HalfSize(GLint components
, GLint width
, GLint height
, const unsigned char *data
, unsigned char *d
, int filter
) {
146 int line
= width
*components
;
148 if (width
> 1 && height
> 1) {
150 for (y
= 0; y
< height
; y
+= 2) {
151 for (x
= 0; x
< width
; x
+= 2) {
152 for (c
= 0; c
< components
; c
++) {
153 *d
++ = (GET(0)+GET(components
)+GET(line
)+GET(line
+components
)) / 4;
161 for (y
= 0; y
< height
; y
+= 2) {
162 for (x
= 0; x
< width
; x
+= 2) {
163 for (c
= 0; c
< components
; c
++) {
172 else if (width
> 1 && height
== 1) {
174 for (y
= 0; y
< height
; y
+= 1) {
175 for (x
= 0; x
< width
; x
+= 2) {
176 for (c
= 0; c
< components
; c
++) {
177 *d
++ = (GET(0)+GET(components
)) / 2;
184 for (y
= 0; y
< height
; y
+= 1) {
185 for (x
= 0; x
< width
; x
+= 2) {
186 for (c
= 0; c
< components
; c
++) {
194 else if (width
== 1 && height
> 1) {
196 for (y
= 0; y
< height
; y
+= 2) {
197 for (x
= 0; x
< width
; x
+= 1) {
198 for (c
= 0; c
< components
; c
++) {
199 *d
++ = (GET(0)+GET(line
)) / 2;
206 for (y
= 0; y
< height
; y
+= 2) {
207 for (x
= 0; x
< width
; x
+= 1) {
208 for (c
= 0; c
< components
; c
++) {
225 /* Replacement for gluBuild2DMipmaps so GLU isn't needed */
226 static void Build2DMipmaps(GLint components
, GLint width
, GLint height
, GLenum format
, const unsigned char *data
, int filter
) {
228 unsigned char *d
= (unsigned char *) malloc((width
/2)*(height
/2)*components
+4);
229 const unsigned char *last
= data
;
231 glTexImage2D(GL_TEXTURE_2D
, level
, components
, width
, height
, 0, format
, GL_UNSIGNED_BYTE
, data
);
234 while (HalfSize(components
, width
, height
, last
, d
, filter
)) {
235 if (width
> 1) width
/= 2;
236 if (height
> 1) height
/= 2;
238 glTexImage2D(GL_TEXTURE_2D
, level
, components
, width
, height
, 0, format
, GL_UNSIGNED_BYTE
, d
);
246 int APIENTRY
pngLoadRaw(const char *filename
, pngRawInfo
*pinfo
) {
248 FILE *fp
= fopen(filename
, "rb");
249 if (fp
== NULL
) return 0;
251 result
= pngLoadRawF(fp
, pinfo
);
253 if (fclose(fp
) != 0) {
256 free(pinfo
->Palette
);
264 int APIENTRY
pngLoadRawF(FILE *fp
, pngRawInfo
*pinfo
) {
265 unsigned char header
[8];
271 png_uint_32 width
, height
, i
;
274 png_bytep data
= NULL
;
275 png_bytep
*row_p
= NULL
;
277 if (pinfo
== NULL
) return 0;
279 if (fread(header
, 1, 8, fp
) != 8) return 0;
280 if (!png_check_sig(header
, 8)) return 0;
282 png
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
284 info
= png_create_info_struct(png
);
286 endinfo
= png_create_info_struct(png
);
287 if (!endinfo
) return 0;
289 if (setjmp(png_jmpbuf(png
)))
292 png_init_io(png
, fp
);
293 png_set_sig_bytes(png
, 8);
294 png_read_info(png
, info
);
295 png_get_IHDR(png
, info
, &width
, &height
, &depth
, &color
, NULL
, NULL
, NULL
);
297 pinfo
->Width
= width
;
298 pinfo
->Height
= height
;
299 pinfo
->Depth
= depth
;
303 if (png_get_gAMA(png
, info
, &fileGamma
))
304 png_set_gamma(png
, screenGamma
, fileGamma
);
306 png_set_gamma(png
, screenGamma
, 1.0/2.2);
308 png_read_update_info(png
, info
);
310 /* HDG: We allocate all the png data in one linear array, thus
311 height * png_get_rowbytes() may not be > PNG_UINT_32_MAX !
312 This check fixes CVE-2010-1519. */
313 if ((uint64_t)height
* png_get_rowbytes(png
, info
) > PNG_UINT_32_MAX
)
316 data
= (png_bytep
) malloc(png_get_rowbytes(png
, info
)*height
);
317 row_p
= (png_bytep
*) malloc(sizeof(png_bytep
)*height
);
321 for (i
= 0; i
< height
; i
++) {
322 if (StandardOrientation
)
323 row_p
[height
- 1 - i
] = &data
[png_get_rowbytes(png
, info
)*i
];
325 row_p
[i
] = &data
[png_get_rowbytes(png
, info
)*i
];
328 png_read_image(png
, row_p
);
332 if (color
== PNG_COLOR_TYPE_PALETTE
) {
334 png_get_PLTE(png
, info
, (png_colorp
*) &pinfo
->Palette
, &cols
);
337 pinfo
->Palette
= NULL
;
340 if (color
&PNG_COLOR_MASK_ALPHA
) {
341 if (color
&PNG_COLOR_MASK_PALETTE
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
)
342 pinfo
->Components
= 2;
344 pinfo
->Components
= 4;
348 if (color
&PNG_COLOR_MASK_PALETTE
|| color
== PNG_COLOR_TYPE_GRAY
)
349 pinfo
->Components
= 1;
351 pinfo
->Components
= 3;
357 png_read_end(png
, endinfo
);
358 png_destroy_read_struct(&png
, &info
, &endinfo
);
362 png_destroy_read_struct(&png
, &info
, &endinfo
);
368 static int pngLoadCommon(int mipmap
, int trans
, pngInfo
*pinfo
, png_structp png
, png_infop info
, png_infop endinfo
) {
372 png_uint_32 width
, height
, rw
, rh
, i
;
375 png_bytep data
= NULL
, data2
= NULL
;
376 png_bytep
*row_p
= NULL
;
380 png_set_sig_bytes(png
, 8);
381 png_read_info(png
, info
);
382 png_get_IHDR(png
, info
, &width
, &height
, &depth
, &color
, NULL
, NULL
, NULL
);
385 pinfo
->Width
= width
;
386 pinfo
->Height
= height
;
387 pinfo
->Depth
= depth
;
390 if (MaxTextureSize
== 0)
391 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, &MaxTextureSize
);
393 #ifdef SUPPORTS_PALETTE_EXT
395 if (PalettedTextures
== -1)
396 PalettedTextures
= ExtSupported("GL_EXT_paletted_texture") && (strstr((const char *) glGetString(GL_VERSION
), "1.1.0 3Dfx Beta") == NULL
);
398 if (PalettedTextures
) {
399 if (glColorTableEXT
== NULL
) {
400 glColorTableEXT
= (PFNGLCOLORTABLEEXTPROC
) wglGetProcAddress("glColorTableEXT");
401 if (glColorTableEXT
== NULL
)
402 PalettedTextures
= 0;
408 if (PalettedTextures
== -1)
409 PalettedTextures
= 0;
411 if (color
== PNG_COLOR_TYPE_GRAY
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
)
412 png_set_gray_to_rgb(png
);
414 if (color
&PNG_COLOR_MASK_ALPHA
&& trans
!= PNG_ALPHA
) {
415 png_set_strip_alpha(png
);
416 color
&= ~PNG_COLOR_MASK_ALPHA
;
419 if (!(PalettedTextures
&& mipmap
>= 0 && trans
== PNG_SOLID
))
420 if (color
== PNG_COLOR_TYPE_PALETTE
)
425 if (png_get_gAMA(png
, info
, &fileGamma
))
426 png_set_gamma(png
, screenGamma
, fileGamma
);
428 png_set_gamma(png
, screenGamma
, 1.0/2.2);
430 png_read_update_info(png
, info
);
432 /* HDG: We allocate all the png data in one linear array, thus
433 height * png_get_rowbytes() may not be > PNG_UINT_32_MAX !
434 This check fixes CVE-2010-1519. */
435 if ((uint64_t)height
* png_get_rowbytes(png
, info
) > PNG_UINT_32_MAX
)
438 data
= (png_bytep
) malloc(png_get_rowbytes(png
, info
)*height
);
439 row_p
= (png_bytep
*) malloc(sizeof(png_bytep
)*height
);
443 for (i
= 0; i
< height
; i
++) {
444 if (StandardOrientation
)
445 row_p
[height
- 1 - i
] = &data
[png_get_rowbytes(png
, info
)*i
];
447 row_p
[i
] = &data
[png_get_rowbytes(png
, info
)*i
];
450 png_read_image(png
, row_p
);
454 rw
= SafeSize(width
), rh
= SafeSize(height
);
456 if (rw
!= width
|| rh
!= height
) {
457 const int channels
= png_get_rowbytes(png
, info
)/width
;
459 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
;
476 glGetIntegerv(GL_PACK_ALIGNMENT
, &pack
);
477 glGetIntegerv(GL_UNPACK_ALIGNMENT
, &unpack
);
478 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
479 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
481 #ifdef SUPPORTS_PALETTE_EXT
482 if (PalettedTextures
&& mipmap
>= 0 && trans
== PNG_SOLID
&& color
== PNG_COLOR_TYPE_PALETTE
) {
487 if (pinfo
!= NULL
) pinfo
->Alpha
= 0;
488 png_get_PLTE(png
, info
, &pal
, &cols
);
491 case 1<<1: intf
= GL_COLOR_INDEX1_EXT
; break;
492 case 1<<2: intf
= GL_COLOR_INDEX2_EXT
; break;
493 case 1<<4: intf
= GL_COLOR_INDEX4_EXT
; break;
494 case 1<<8: intf
= GL_COLOR_INDEX8_EXT
; break;
495 case 1<<12: intf
= GL_COLOR_INDEX12_EXT
; break;
496 case 1<<16: intf
= GL_COLOR_INDEX16_EXT
; break;
498 /*printf("Warning: Colour depth %i not recognised\n", cols);*/
501 glColorTableEXT(GL_TEXTURE_2D
, GL_RGB8
, cols
, GL_RGB
, GL_UNSIGNED_BYTE
, pal
);
502 glTexImage2D(GL_TEXTURE_2D
, mipmap
, intf
, width
, height
, 0, GL_COLOR_INDEX
, GL_UNSIGNED_BYTE
, data
);
506 if (trans
== PNG_SOLID
|| trans
== PNG_ALPHA
|| trans
== PNG_LUMINANCEALPHA
|| color
== PNG_COLOR_TYPE_RGB_ALPHA
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
) {
511 case PNG_COLOR_TYPE_GRAY
:
512 case PNG_COLOR_TYPE_RGB
:
513 case PNG_COLOR_TYPE_PALETTE
:
516 if (pinfo
!= NULL
) pinfo
->Alpha
= 0;
519 case PNG_COLOR_TYPE_GRAY_ALPHA
:
520 case PNG_COLOR_TYPE_RGB_ALPHA
:
523 if (pinfo
!= NULL
) pinfo
->Alpha
= 8;
527 /*puts("glformat not set");*/
531 if (trans
== PNG_LUMINANCEALPHA
)
532 glformat
= GL_LUMINANCE_ALPHA
;
534 if (mipmap
== PNG_BUILDMIPMAPS
)
535 Build2DMipmaps(glcomponent
, width
, height
, glformat
, data
, 1);
536 else if (mipmap
== PNG_SIMPLEMIPMAPS
)
537 Build2DMipmaps(glcomponent
, width
, height
, glformat
, data
, 0);
539 glTexImage2D(GL_TEXTURE_2D
, mipmap
, glcomponent
, width
, height
, 0, glformat
, GL_UNSIGNED_BYTE
, data
);
542 png_bytep p
, endp
, q
;
545 /* HDG another potential 32 bit address overflow, the
546 original png had 3 channels and we are going to
548 if ((uint64_t)width
* height
> (PNG_UINT_32_MAX
>> 2))
551 p
= data
, endp
= p
+width
*height
*3;
552 q
= data2
= (png_bytep
) malloc(sizeof(png_byte
)*width
*height
*4);
554 if (pinfo
!= NULL
) pinfo
->Alpha
= 8;
559 g = *p++; /*green*/ \
560 b = *p++; /*blue */ \
574 ALPHA
= AlphaCallback((unsigned char) r
, (unsigned char) g
, (unsigned char) b
);
580 if (r
== StencilRed
&& g
== StencilGreen
&& b
== StencilBlue
)
590 if (a
> 255) ALPHA
= 255; else ALPHA
= a
;
597 if (a
> 255*2) ALPHA
= 255; else ALPHA
= a
/2;
610 if (a
> 255) ALPHA
= 255; else ALPHA
= a
;
617 if (a
> 255*2) ALPHA
= 255; else ALPHA
= a
/2;
624 if (a
> 255*3) ALPHA
= 255; else ALPHA
= a
/3;
631 if (a
> 255*255) ALPHA
= 255; else ALPHA
= (int) sqrt(a
);
640 if (mipmap
== PNG_BUILDMIPMAPS
)
641 Build2DMipmaps(4, width
, height
, GL_RGBA
, data2
, 1);
642 else if (mipmap
== PNG_SIMPLEMIPMAPS
)
643 Build2DMipmaps(4, width
, height
, GL_RGBA
, data2
, 0);
645 glTexImage2D(GL_TEXTURE_2D
, mipmap
, 4, width
, height
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, data2
);
649 glPixelStorei(GL_PACK_ALIGNMENT
, pack
);
650 glPixelStorei(GL_UNPACK_ALIGNMENT
, unpack
);
653 png_read_end(png
, endinfo
);
663 int APIENTRY
pngLoad(const char *filename
, int mipmap
, int trans
, pngInfo
*pinfo
) {
665 FILE *fp
= fopen(filename
, "rb");
666 if (fp
== NULL
) return 0;
668 result
= pngLoadF(fp
, mipmap
, trans
, pinfo
);
670 if (fclose(fp
) != 0) return 0;
675 int APIENTRY
pngLoadF(FILE *fp
, int mipmap
, int trans
, pngInfo
*pinfo
) {
676 unsigned char header
[8];
683 if (fread(header
, 1, 8, fp
) != 8) return 0;
684 if (!png_check_sig(header
, 8)) return 0;
686 png
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
688 info
= png_create_info_struct(png
);
690 endinfo
= png_create_info_struct(png
);
691 if (!endinfo
) return 0;
693 if (!setjmp(png_jmpbuf(png
))) {
694 png_init_io(png
, fp
);
695 ret
= pngLoadCommon(mipmap
, trans
, pinfo
, png
, info
, endinfo
);
698 png_destroy_read_struct(&png
, &info
, &endinfo
);
702 typedef struct glpng_memread_struct
{
707 static void glpng_read_mem(png_structp png
, png_bytep dst
, png_size_t size
) {
708 glpng_memread
*mr
= (glpng_memread
*) png_get_io_ptr(png
);
709 memcpy(dst
, mr
->mem
+ mr
->rpos
, size
);
713 int APIENTRY
pngLoadMem(const char *mem
, int size
, int mipmap
, int trans
, pngInfo
*pinfo
) {
714 unsigned char header
[8];
718 glpng_memread memread
;
725 memcpy(header
, mem
, 8);
726 if (!png_check_sig(header
, 8)) return 0;
728 png
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
730 info
= png_create_info_struct(png
);
732 endinfo
= png_create_info_struct(png
);
733 if (!endinfo
) return 0;
735 if (!setjmp(png_jmpbuf(png
))) {
737 memread
.mem
= ((png_bytep
) mem
) + 8;
738 png_set_read_fn(png
, &memread
, glpng_read_mem
);
739 ret
= pngLoadCommon(mipmap
, trans
, pinfo
, png
, info
, endinfo
);
742 png_destroy_read_struct(&png
, &info
, &endinfo
);
746 static unsigned int SetParams(int wrapst
, int magfilter
, int minfilter
) {
749 glGenTextures(1, &id
);
750 glBindTexture(GL_TEXTURE_2D
, id
);
752 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, wrapst
);
753 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, wrapst
);
755 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, magfilter
);
756 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, minfilter
);
761 unsigned int APIENTRY
pngBind(const char *filename
, int mipmap
, int trans
, pngInfo
*info
, int wrapst
, int minfilter
, int magfilter
) {
762 unsigned int id
= SetParams(wrapst
, magfilter
, minfilter
);
764 if (id
!= 0 && pngLoad(filename
, mipmap
, trans
, info
))
769 unsigned int APIENTRY
pngBindF(FILE *file
, int mipmap
, int trans
, pngInfo
*info
, int wrapst
, int minfilter
, int magfilter
) {
770 unsigned int id
= SetParams(wrapst
, magfilter
, minfilter
);
772 if (id
!= 0 && pngLoadF(file
, mipmap
, trans
, info
))
777 unsigned int APIENTRY
pngBindMem(const char *mem
, int size
, int mipmap
, int trans
, pngInfo
*info
, int wrapst
, int minfilter
, int magfilter
) {
778 unsigned int id
= SetParams(wrapst
, magfilter
, minfilter
);
780 if (id
!= 0 && pngLoadMem(mem
, size
, mipmap
, trans
, info
))
785 void APIENTRY
pngSetStencil(unsigned char red
, unsigned char green
, unsigned char blue
) {
786 StencilRed
= red
, StencilGreen
= green
, StencilBlue
= blue
;
789 void APIENTRY
pngSetAlphaCallback(unsigned char (*callback
)(unsigned char red
, unsigned char green
, unsigned char blue
)) {
790 if (callback
== NULL
)
791 AlphaCallback
= DefaultAlphaCallback
;
793 AlphaCallback
= callback
;
796 void APIENTRY
pngSetViewingGamma(double viewingGamma
) {
797 if(viewingGamma
> 0) {
799 screenGamma
= 2.2/viewingGamma
;
807 void APIENTRY
pngSetStandardOrientation(int standardorientation
) {
808 StandardOrientation
= standardorientation
;