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
++)
122 static int ExtSupported(const char *x
) {
123 static const GLubyte
*ext
= NULL
;
125 int xlen
= strlen(x
);
127 if (ext
== NULL
) ext
= glGetString(GL_EXTENSIONS
);
129 c
= (const char*)ext
;
132 if (strcmp(c
, x
) == 0 && (c
[xlen
] == '\0' || c
[xlen
] == ' ')) return 1;
140 #define GET(o) ((int)*(data + (o)))
142 static int HalfSize(GLint components
, GLint width
, GLint height
, const unsigned char *data
, unsigned char *d
, int filter
) {
144 int line
= width
*components
;
146 if (width
> 1 && height
> 1) {
148 for (y
= 0; y
< height
; y
+= 2) {
149 for (x
= 0; x
< width
; x
+= 2) {
150 for (c
= 0; c
< components
; c
++) {
151 *d
++ = (GET(0)+GET(components
)+GET(line
)+GET(line
+components
)) / 4;
159 for (y
= 0; y
< height
; y
+= 2) {
160 for (x
= 0; x
< width
; x
+= 2) {
161 for (c
= 0; c
< components
; c
++) {
170 else if (width
> 1 && height
== 1) {
172 for (y
= 0; y
< height
; y
+= 1) {
173 for (x
= 0; x
< width
; x
+= 2) {
174 for (c
= 0; c
< components
; c
++) {
175 *d
++ = (GET(0)+GET(components
)) / 2;
182 for (y
= 0; y
< height
; y
+= 1) {
183 for (x
= 0; x
< width
; x
+= 2) {
184 for (c
= 0; c
< components
; c
++) {
192 else if (width
== 1 && height
> 1) {
194 for (y
= 0; y
< height
; y
+= 2) {
195 for (x
= 0; x
< width
; x
+= 1) {
196 for (c
= 0; c
< components
; c
++) {
197 *d
++ = (GET(0)+GET(line
)) / 2;
204 for (y
= 0; y
< height
; y
+= 2) {
205 for (x
= 0; x
< width
; x
+= 1) {
206 for (c
= 0; c
< components
; c
++) {
223 /* Replacement for gluBuild2DMipmaps so GLU isn't needed */
224 static void Build2DMipmaps(GLint components
, GLint width
, GLint height
, GLenum format
, const unsigned char *data
, int filter
) {
226 unsigned char *d
= (unsigned char *) malloc((width
/2)*(height
/2)*components
+4);
227 const unsigned char *last
= data
;
229 glTexImage2D(GL_TEXTURE_2D
, level
, components
, width
, height
, 0, format
, GL_UNSIGNED_BYTE
, data
);
232 while (HalfSize(components
, width
, height
, last
, d
, filter
)) {
233 if (width
> 1) width
/= 2;
234 if (height
> 1) height
/= 2;
236 glTexImage2D(GL_TEXTURE_2D
, level
, components
, width
, height
, 0, format
, GL_UNSIGNED_BYTE
, d
);
244 int APIENTRY
pngLoadRaw(const char *filename
, pngRawInfo
*pinfo
) {
246 FILE *fp
= fopen(filename
, "rb");
247 if (fp
== NULL
) return 0;
249 result
= pngLoadRawF(fp
, pinfo
);
251 if (fclose(fp
) != 0) {
254 free(pinfo
->Palette
);
262 int APIENTRY
pngLoadRawF(FILE *fp
, pngRawInfo
*pinfo
) {
263 unsigned char header
[8];
269 png_uint_32 width
, height
, i
;
272 png_bytep data
= NULL
;
273 png_bytep
*row_p
= NULL
;
275 if (pinfo
== NULL
) return 0;
277 fread(header
, 1, 8, fp
);
278 if (!png_check_sig(header
, 8)) return 0;
280 png
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
282 info
= png_create_info_struct(png
);
284 endinfo
= png_create_info_struct(png
);
285 if (!endinfo
) return 0;
287 if (setjmp(png_jmpbuf(png
)))
290 png_init_io(png
, fp
);
291 png_set_sig_bytes(png
, 8);
292 png_read_info(png
, info
);
293 png_get_IHDR(png
, info
, &width
, &height
, &depth
, &color
, NULL
, NULL
, NULL
);
295 pinfo
->Width
= width
;
296 pinfo
->Height
= height
;
297 pinfo
->Depth
= depth
;
301 if (png_get_gAMA(png
, info
, &fileGamma
))
302 png_set_gamma(png
, screenGamma
, fileGamma
);
304 png_set_gamma(png
, screenGamma
, 1.0/2.2);
306 png_read_update_info(png
, info
);
308 /* HDG: We allocate all the png data in one linear array, thus
309 height * png_get_rowbytes() may not be > PNG_UINT_32_MAX !
310 This check fixes CVE-2010-1519. */
311 if ((uint64_t)height
* png_get_rowbytes(png
, info
) > PNG_UINT_32_MAX
)
314 data
= (png_bytep
) malloc(png_get_rowbytes(png
, info
)*height
);
315 row_p
= (png_bytep
*) malloc(sizeof(png_bytep
)*height
);
319 for (i
= 0; i
< height
; i
++) {
320 if (StandardOrientation
)
321 row_p
[height
- 1 - i
] = &data
[png_get_rowbytes(png
, info
)*i
];
323 row_p
[i
] = &data
[png_get_rowbytes(png
, info
)*i
];
326 png_read_image(png
, row_p
);
330 if (color
== PNG_COLOR_TYPE_PALETTE
) {
332 png_get_PLTE(png
, info
, (png_colorp
*) &pinfo
->Palette
, &cols
);
335 pinfo
->Palette
= NULL
;
338 if (color
&PNG_COLOR_MASK_ALPHA
) {
339 if (color
&PNG_COLOR_MASK_PALETTE
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
)
340 pinfo
->Components
= 2;
342 pinfo
->Components
= 4;
346 if (color
&PNG_COLOR_MASK_PALETTE
|| color
== PNG_COLOR_TYPE_GRAY
)
347 pinfo
->Components
= 1;
349 pinfo
->Components
= 3;
355 png_read_end(png
, endinfo
);
356 png_destroy_read_struct(&png
, &info
, &endinfo
);
360 png_destroy_read_struct(&png
, &info
, &endinfo
);
366 static int pngLoadCommon(int mipmap
, int trans
, pngInfo
*pinfo
, png_structp png
, png_infop info
, png_infop endinfo
) {
370 png_uint_32 width
, height
, rw
, rh
, i
;
373 png_bytep data
= NULL
, data2
= NULL
;
374 png_bytep
*row_p
= NULL
;
378 png_set_sig_bytes(png
, 8);
379 png_read_info(png
, info
);
380 png_get_IHDR(png
, info
, &width
, &height
, &depth
, &color
, NULL
, NULL
, NULL
);
383 pinfo
->Width
= width
;
384 pinfo
->Height
= height
;
385 pinfo
->Depth
= depth
;
388 if (MaxTextureSize
== 0)
389 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, &MaxTextureSize
);
391 #ifdef SUPPORTS_PALETTE_EXT
393 if (PalettedTextures
== -1)
394 PalettedTextures
= ExtSupported("GL_EXT_paletted_texture") && (strstr((const char *) glGetString(GL_VERSION
), "1.1.0 3Dfx Beta") == NULL
);
396 if (PalettedTextures
) {
397 if (glColorTableEXT
== NULL
) {
398 glColorTableEXT
= (PFNGLCOLORTABLEEXTPROC
) wglGetProcAddress("glColorTableEXT");
399 if (glColorTableEXT
== NULL
)
400 PalettedTextures
= 0;
406 if (PalettedTextures
== -1)
407 PalettedTextures
= 0;
409 if (color
== PNG_COLOR_TYPE_GRAY
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
)
410 png_set_gray_to_rgb(png
);
412 if (color
&PNG_COLOR_MASK_ALPHA
&& trans
!= PNG_ALPHA
) {
413 png_set_strip_alpha(png
);
414 color
&= ~PNG_COLOR_MASK_ALPHA
;
417 if (!(PalettedTextures
&& mipmap
>= 0 && trans
== PNG_SOLID
))
418 if (color
== PNG_COLOR_TYPE_PALETTE
)
423 if (png_get_gAMA(png
, info
, &fileGamma
))
424 png_set_gamma(png
, screenGamma
, fileGamma
);
426 png_set_gamma(png
, screenGamma
, 1.0/2.2);
428 png_read_update_info(png
, info
);
430 /* HDG: We allocate all the png data in one linear array, thus
431 height * png_get_rowbytes() may not be > PNG_UINT_32_MAX !
432 This check fixes CVE-2010-1519. */
433 if ((uint64_t)height
* png_get_rowbytes(png
, info
) > PNG_UINT_32_MAX
)
436 data
= (png_bytep
) malloc(png_get_rowbytes(png
, info
)*height
);
437 row_p
= (png_bytep
*) malloc(sizeof(png_bytep
)*height
);
441 for (i
= 0; i
< height
; i
++) {
442 if (StandardOrientation
)
443 row_p
[height
- 1 - i
] = &data
[png_get_rowbytes(png
, info
)*i
];
445 row_p
[i
] = &data
[png_get_rowbytes(png
, info
)*i
];
448 png_read_image(png
, row_p
);
452 rw
= SafeSize(width
), rh
= SafeSize(height
);
454 if (rw
!= width
|| rh
!= height
) {
455 const int channels
= png_get_rowbytes(png
, info
)/width
;
457 data2
= (png_bytep
) malloc(rw
*rh
*channels
);
461 /* Doesn't work on certain sizes */
462 /* if (gluScaleImage(glformat, width, height, GL_UNSIGNED_BYTE, data, rw, rh, GL_UNSIGNED_BYTE, data2) != 0)
465 Resize(channels
, data
, width
, height
, data2
, rw
, rh
);
467 width
= rw
, height
= rh
;
474 glGetIntegerv(GL_PACK_ALIGNMENT
, &pack
);
475 glGetIntegerv(GL_UNPACK_ALIGNMENT
, &unpack
);
476 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
477 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
479 #ifdef SUPPORTS_PALETTE_EXT
480 if (PalettedTextures
&& mipmap
>= 0 && trans
== PNG_SOLID
&& color
== PNG_COLOR_TYPE_PALETTE
) {
485 if (pinfo
!= NULL
) pinfo
->Alpha
= 0;
486 png_get_PLTE(png
, info
, &pal
, &cols
);
489 case 1<<1: intf
= GL_COLOR_INDEX1_EXT
; break;
490 case 1<<2: intf
= GL_COLOR_INDEX2_EXT
; break;
491 case 1<<4: intf
= GL_COLOR_INDEX4_EXT
; break;
492 case 1<<8: intf
= GL_COLOR_INDEX8_EXT
; break;
493 case 1<<12: intf
= GL_COLOR_INDEX12_EXT
; break;
494 case 1<<16: intf
= GL_COLOR_INDEX16_EXT
; break;
496 /*printf("Warning: Colour depth %i not recognised\n", cols);*/
499 glColorTableEXT(GL_TEXTURE_2D
, GL_RGB8
, cols
, GL_RGB
, GL_UNSIGNED_BYTE
, pal
);
500 glTexImage2D(GL_TEXTURE_2D
, mipmap
, intf
, width
, height
, 0, GL_COLOR_INDEX
, GL_UNSIGNED_BYTE
, data
);
504 if (trans
== PNG_SOLID
|| trans
== PNG_ALPHA
|| color
== PNG_COLOR_TYPE_RGB_ALPHA
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
) {
509 case PNG_COLOR_TYPE_GRAY
:
510 case PNG_COLOR_TYPE_RGB
:
511 case PNG_COLOR_TYPE_PALETTE
:
514 if (pinfo
!= NULL
) pinfo
->Alpha
= 0;
517 case PNG_COLOR_TYPE_GRAY_ALPHA
:
518 case PNG_COLOR_TYPE_RGB_ALPHA
:
521 if (pinfo
!= NULL
) pinfo
->Alpha
= 8;
525 /*puts("glformat not set");*/
529 if (mipmap
== PNG_BUILDMIPMAPS
)
530 Build2DMipmaps(glcomponent
, width
, height
, glformat
, data
, 1);
531 else if (mipmap
== PNG_SIMPLEMIPMAPS
)
532 Build2DMipmaps(glcomponent
, width
, height
, glformat
, data
, 0);
534 glTexImage2D(GL_TEXTURE_2D
, mipmap
, glcomponent
, width
, height
, 0, glformat
, GL_UNSIGNED_BYTE
, data
);
537 png_bytep p
, endp
, q
;
540 /* HDG another potential 32 bit address overflow, the
541 original png had 3 channels and we are going to
543 if ((uint64_t)width
* height
> (PNG_UINT_32_MAX
>> 2))
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
);
644 glPixelStorei(GL_PACK_ALIGNMENT
, pack
);
645 glPixelStorei(GL_UNPACK_ALIGNMENT
, unpack
);
648 png_read_end(png
, endinfo
);
658 int APIENTRY
pngLoad(const char *filename
, int mipmap
, int trans
, pngInfo
*pinfo
) {
660 FILE *fp
= fopen(filename
, "rb");
661 if (fp
== NULL
) return 0;
663 result
= pngLoadF(fp
, mipmap
, trans
, pinfo
);
665 if (fclose(fp
) != 0) return 0;
670 int APIENTRY
pngLoadF(FILE *fp
, int mipmap
, int trans
, pngInfo
*pinfo
) {
671 unsigned char header
[8];
678 fread(header
, 1, 8, fp
);
679 if (!png_check_sig(header
, 8)) return 0;
681 png
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
683 info
= png_create_info_struct(png
);
685 endinfo
= png_create_info_struct(png
);
686 if (!endinfo
) return 0;
688 if (!setjmp(png_jmpbuf(png
))) {
689 png_init_io(png
, fp
);
690 ret
= pngLoadCommon(mipmap
, trans
, pinfo
, png
, info
, endinfo
);
693 png_destroy_read_struct(&png
, &info
, &endinfo
);
697 typedef struct glpng_memread_struct
{
702 static void glpng_read_mem(png_structp png
, png_bytep dst
, png_size_t size
) {
703 glpng_memread
*mr
= (glpng_memread
*) png_get_io_ptr(png
);
704 memcpy(dst
, mr
->mem
+ mr
->rpos
, size
);
708 int APIENTRY
pngLoadMem(const char *mem
, int size
, int mipmap
, int trans
, pngInfo
*pinfo
) {
709 unsigned char header
[8];
713 glpng_memread memread
;
720 memcpy(header
, mem
, 8);
721 if (!png_check_sig(header
, 8)) return 0;
723 png
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
725 info
= png_create_info_struct(png
);
727 endinfo
= png_create_info_struct(png
);
728 if (!endinfo
) return 0;
730 if (!setjmp(png_jmpbuf(png
))) {
732 memread
.mem
= ((png_bytep
) mem
) + 8;
733 png_set_read_fn(png
, &memread
, glpng_read_mem
);
734 ret
= pngLoadCommon(mipmap
, trans
, pinfo
, png
, info
, endinfo
);
737 png_destroy_read_struct(&png
, &info
, &endinfo
);
741 static unsigned int SetParams(int wrapst
, int magfilter
, int minfilter
) {
744 glGenTextures(1, &id
);
745 glBindTexture(GL_TEXTURE_2D
, id
);
747 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, wrapst
);
748 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, wrapst
);
750 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, magfilter
);
751 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, minfilter
);
756 unsigned int APIENTRY
pngBind(const char *filename
, int mipmap
, int trans
, pngInfo
*info
, int wrapst
, int minfilter
, int magfilter
) {
757 unsigned int id
= SetParams(wrapst
, magfilter
, minfilter
);
759 if (id
!= 0 && pngLoad(filename
, mipmap
, trans
, info
))
764 unsigned int APIENTRY
pngBindF(FILE *file
, int mipmap
, int trans
, pngInfo
*info
, int wrapst
, int minfilter
, int magfilter
) {
765 unsigned int id
= SetParams(wrapst
, magfilter
, minfilter
);
767 if (id
!= 0 && pngLoadF(file
, mipmap
, trans
, info
))
772 unsigned int APIENTRY
pngBindMem(const char *mem
, int size
, int mipmap
, int trans
, pngInfo
*info
, int wrapst
, int minfilter
, int magfilter
) {
773 unsigned int id
= SetParams(wrapst
, magfilter
, minfilter
);
775 if (id
!= 0 && pngLoadMem(mem
, size
, mipmap
, trans
, info
))
780 void APIENTRY
pngSetStencil(unsigned char red
, unsigned char green
, unsigned char blue
) {
781 StencilRed
= red
, StencilGreen
= green
, StencilBlue
= blue
;
784 void APIENTRY
pngSetAlphaCallback(unsigned char (*callback
)(unsigned char red
, unsigned char green
, unsigned char blue
)) {
785 if (callback
== NULL
)
786 AlphaCallback
= DefaultAlphaCallback
;
788 AlphaCallback
= callback
;
791 void APIENTRY
pngSetViewingGamma(double viewingGamma
) {
792 if(viewingGamma
> 0) {
794 screenGamma
= 2.2/viewingGamma
;
802 void APIENTRY
pngSetStandardOrientation(int standardorientation
) {
803 StandardOrientation
= standardorientation
;