4bbf238470a47047e78afa22a7c870dc3d9b05fb
[glpng.git] / src / glpng.c
blob4bbf238470a47047e78afa22a7c870dc3d9b05fb
1 /*
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 */
24 #undef FAR
25 #include <windows.h>
26 #endif
28 #define GL_GLEXT_PROTOTYPES
30 #include <GL/glpng.h>
31 #include <GL/gl.h>
32 #include <GL/glext.h>
33 #include <stdlib.h>
34 #include <math.h>
35 #include <png.h>
37 /* Used to decide if GL/gl.h supports the paletted extension */
38 #ifdef GL_COLOR_INDEX1_EXT
39 #define SUPPORTS_PALETTE_EXT
40 #endif
42 static unsigned char DefaultAlphaCallback(unsigned char red, unsigned char green, unsigned char blue) {
43 return 255;
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
51 #ifdef _WIN32
52 static PFNGLCOLORTABLEEXTPROC glColorTableEXT = NULL;
53 #endif
54 #endif
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.
65 #ifdef _MAC
66 static double screenGamma = 2.2 / 1.45;
67 #elif SGI
68 static double screenGamma = 2.2 / 1.7;
69 #else /* PC/default */
70 static double screenGamma = 2.2 / 1.0;
71 #endif
73 static char gammaExplicit = 0; /*if */
75 static void checkForGammaEnv()
77 double viewingGamma;
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) {
89 int p;
91 if (i > MaxTextureSize) return MaxTextureSize;
93 for (p = 0; p < 24; p++)
94 if (i <= (1<<p))
95 return 1<<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;
103 int x, y, xx, yy, c;
104 png_bytep d;
106 for (y = 0; y < h2; y++) {
107 yy = (int) (y*sy)*w1;
109 for (x = 0; x < w2; x++) {
110 xx = (int) (x*sx);
111 d = d1 + (yy+xx)*components;
113 for (c = 0; c < components; c++)
114 *d2++ = *d++;
119 #ifdef _WIN32
120 static int ExtSupported(const char *x) {
121 static const GLubyte *ext = NULL;
122 const char *c;
123 int xlen = strlen(x);
125 if (ext == NULL) ext = glGetString(GL_EXTENSIONS);
127 c = (const char*)ext;
129 while (*c != '\0') {
130 if (strcmp(c, x) == 0 && (c[xlen] == '\0' || c[xlen] == ' ')) return 1;
131 c++;
134 return 0;
136 #endif
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) {
141 int x, y, c;
142 int line = width*components;
144 if (width > 1 && height > 1) {
145 if (filter)
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;
150 data++;
152 data += components;
154 data += line;
156 else
157 for (y = 0; y < height; y += 2) {
158 for (x = 0; x < width; x += 2) {
159 for (c = 0; c < components; c++) {
160 *d++ = GET(0);
161 data++;
163 data += components;
165 data += line;
168 else if (width > 1 && height == 1) {
169 if (filter)
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;
174 data++;
176 data += components;
179 else
180 for (y = 0; y < height; y += 1) {
181 for (x = 0; x < width; x += 2) {
182 for (c = 0; c < components; c++) {
183 *d++ = GET(0);
184 data++;
186 data += components;
190 else if (width == 1 && height > 1) {
191 if (filter)
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;
196 data++;
199 data += line;
201 else
202 for (y = 0; y < height; y += 2) {
203 for (x = 0; x < width; x += 1) {
204 for (c = 0; c < components; c++) {
205 *d++ = GET(0);
206 data++;
209 data += line;
212 else {
213 return 0;
216 return 1;
219 #undef GET
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) {
223 int level = 0;
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);
228 level++;
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);
235 level++;
236 last = d;
239 free(d);
242 int APIENTRY pngLoadRaw(const char *filename, pngRawInfo *pinfo) {
243 int result;
244 FILE *fp = fopen(filename, "rb");
245 if (fp == NULL) return 0;
247 result = pngLoadRawF(fp, pinfo);
249 if (fclose(fp) != 0) {
250 if (result) {
251 free(pinfo->Data);
252 free(pinfo->Palette);
254 return 0;
257 return result;
260 int APIENTRY pngLoadRawF(FILE *fp, pngRawInfo *pinfo) {
261 unsigned char header[8];
262 png_structp png;
263 png_infop info;
264 png_infop endinfo;
265 png_bytep data;
266 png_bytep *row_p;
267 double fileGamma;
269 png_uint_32 width, height;
270 int depth, color;
272 png_uint_32 i;
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(png)))
286 png_destroy_read_struct(&png, &info, &endinfo);
287 return 0;
289 // ~DH
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;
300 /*--GAMMA--*/
301 checkForGammaEnv();
302 if (png_get_gAMA(png, info, &fileGamma))
303 png_set_gamma(png, screenGamma, fileGamma);
304 else
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];
315 else
316 row_p[i] = &data[png_get_rowbytes(png, info)*i];
319 png_read_image(png, row_p);
320 free(row_p);
322 if (color == PNG_COLOR_TYPE_PALETTE) {
323 int cols;
324 png_get_PLTE(png, info, (png_colorp *) &pinfo->Palette, &cols);
326 else {
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;
333 else
334 pinfo->Components = 4;
335 pinfo->Alpha = 8;
337 else {
338 if (color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY)
339 pinfo->Components = 1;
340 else
341 pinfo->Components = 3;
342 pinfo->Alpha = 0;
345 pinfo->Data = data;
347 png_read_end(png, endinfo);
348 png_destroy_read_struct(&png, &info, &endinfo);
350 return 1;
353 int APIENTRY pngLoad(const char *filename, int mipmap, int trans, pngInfo *pinfo) {
354 int result;
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;
362 return result;
365 int APIENTRY pngLoadF(FILE *fp, int mipmap, int trans, pngInfo *pinfo) {
366 GLint pack, unpack;
367 unsigned char header[8];
368 png_structp png;
369 png_infop info;
370 png_infop endinfo;
371 png_bytep data, data2;
372 png_bytep *row_p;
373 double fileGamma;
375 png_uint_32 width, height, rw, rh;
376 int depth, color;
378 png_uint_32 i;
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(png)))
390 png_destroy_read_struct(&png, &info, &endinfo);
391 return 0;
393 // ~DH
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);
400 if (pinfo != 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
410 #ifdef _WIN32
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;
421 #endif
422 #endif
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)
437 png_set_expand(png);
439 /*--GAMMA--*/
440 checkForGammaEnv();
441 if (png_get_gAMA(png, info, &fileGamma))
442 png_set_gamma(png, screenGamma, fileGamma);
443 else
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];
454 else
455 row_p[i] = &data[png_get_rowbytes(png, info)*i];
458 png_read_image(png, row_p);
459 free(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)
470 return 0;
472 Resize(channels, data, width, height, data2, rw, rh);
474 width = rw, height = rh;
475 free(data);
476 data = data2;
479 { /* OpenGL stuff */
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) {
487 png_colorp pal;
488 int cols;
489 GLint intf;
491 if (pinfo != NULL) pinfo->Alpha = 0;
492 png_get_PLTE(png, info, &pal, &cols);
494 switch (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;
501 default:
502 /*printf("Warning: Colour depth %i not recognised\n", cols);*/
503 return 0;
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);
508 else
509 #endif
510 if (trans == PNG_SOLID || trans == PNG_ALPHA || color == PNG_COLOR_TYPE_RGB_ALPHA || color == PNG_COLOR_TYPE_GRAY_ALPHA) {
511 GLenum glformat;
512 GLint glcomponent;
514 switch (color) {
515 case PNG_COLOR_TYPE_GRAY:
516 case PNG_COLOR_TYPE_RGB:
517 case PNG_COLOR_TYPE_PALETTE:
518 glformat = GL_RGB;
519 glcomponent = 3;
520 if (pinfo != NULL) pinfo->Alpha = 0;
521 break;
523 case PNG_COLOR_TYPE_GRAY_ALPHA:
524 case PNG_COLOR_TYPE_RGB_ALPHA:
525 glformat = GL_RGBA;
526 glcomponent = 4;
527 if (pinfo != NULL) pinfo->Alpha = 8;
528 break;
530 default:
531 /*puts("glformat not set");*/
532 return 0;
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);
539 else
540 glTexImage2D(GL_TEXTURE_2D, mipmap, glcomponent, width, height, 0, glformat, GL_UNSIGNED_BYTE, data);
542 else {
543 png_bytep p, endp, q;
544 int r, g, b, a;
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;
551 #define FORSTART \
552 do { \
553 r = *p++; /*red */ \
554 g = *p++; /*green*/ \
555 b = *p++; /*blue */ \
556 *q++ = r; \
557 *q++ = g; \
558 *q++ = b;
560 #define FOREND \
561 q++; \
562 } while (p != endp);
564 #define ALPHA *q
566 switch (trans) {
567 case PNG_CALLBACKT:
568 FORSTART
569 ALPHA = AlphaCallback((unsigned char) r, (unsigned char) g, (unsigned char) b);
570 FOREND
571 break;
573 case PNG_STENCIL:
574 FORSTART
575 if (r == StencilRed && g == StencilGreen && b == StencilBlue)
576 ALPHA = 0;
577 else
578 ALPHA = 255;
579 FOREND
580 break;
582 case PNG_BLEND1:
583 FORSTART
584 a = r+g+b;
585 if (a > 255) ALPHA = 255; else ALPHA = a;
586 FOREND
587 break;
589 case PNG_BLEND2:
590 FORSTART
591 a = r+g+b;
592 if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
593 FOREND
594 break;
596 case PNG_BLEND3:
597 FORSTART
598 ALPHA = (r+g+b)/3;
599 FOREND
600 break;
602 case PNG_BLEND4:
603 FORSTART
604 a = r*r+g*g+b*b;
605 if (a > 255) ALPHA = 255; else ALPHA = a;
606 FOREND
607 break;
609 case PNG_BLEND5:
610 FORSTART
611 a = r*r+g*g+b*b;
612 if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
613 FOREND
614 break;
616 case PNG_BLEND6:
617 FORSTART
618 a = r*r+g*g+b*b;
619 if (a > 255*3) ALPHA = 255; else ALPHA = a/3;
620 FOREND
621 break;
623 case PNG_BLEND7:
624 FORSTART
625 a = r*r+g*g+b*b;
626 if (a > 255*255) ALPHA = 255; else ALPHA = (int) sqrt(a);
627 FOREND
628 break;
631 #undef FORSTART
632 #undef FOREND
633 #undef ALPHA
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);
639 else
640 glTexImage2D(GL_TEXTURE_2D, mipmap, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);
642 free(data2);
645 glPixelStorei(GL_PACK_ALIGNMENT, pack);
646 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
647 } /* OpenGL end */
649 png_read_end(png, endinfo);
650 png_destroy_read_struct(&png, &info, &endinfo);
652 free(data);
654 return 1;
657 static unsigned int SetParams(int wrapst, int magfilter, int minfilter) {
658 unsigned int id;
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);
669 return id;
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))
676 return id;
677 return 0;
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))
684 return id;
685 return 0;
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;
695 else
696 AlphaCallback = callback;
699 void APIENTRY pngSetViewingGamma(double viewingGamma) {
700 if(viewingGamma > 0) {
701 gammaExplicit = 1;
702 screenGamma = 2.2/viewingGamma;
704 else {
705 gammaExplicit = 0;
706 screenGamma = 2.2;
710 void APIENTRY pngSetStandardOrientation(int standardorientation) {
711 StandardOrientation = standardorientation;