edae8f5bd06c97a90e0dc2ca4316a65695c1c382
[glpng.git] / src / glpng.c
blobedae8f5bd06c97a90e0dc2ca4316a65695c1c382
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 #include <GL/glpng.h>
29 #include <GL/gl.h>
30 #include <stdlib.h>
31 #include <math.h>
32 #include <png.h>
34 /* Used to decide if GL/gl.h supports the paletted extension */
35 #ifdef GL_COLOR_INDEX1_EXT
36 #define SUPPORTS_PALETTE_EXT
37 #endif
39 static unsigned char DefaultAlphaCallback(unsigned char red, unsigned char green, unsigned char blue) {
40 return 255;
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
48 #ifdef _WIN32
49 static PFNGLCOLORTABLEEXTPROC glColorTableEXT = NULL;
50 #endif
51 #endif
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.
62 #ifdef _MAC
63 static double screenGamma = 2.2 / 1.45;
64 #elif SGI
65 static double screenGamma = 2.2 / 1.7;
66 #else /* PC/default */
67 static double screenGamma = 2.2 / 1.0;
68 #endif
70 static char gammaExplicit = 0; /*if */
72 static void checkForGammaEnv()
74 double viewingGamma;
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) {
86 int p;
88 if (i > MaxTextureSize) return MaxTextureSize;
90 for (p = 0; p < 24; p++)
91 if (i <= (1<<p))
92 return 1<<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;
100 int x, y, xx, yy, c;
101 png_bytep d;
103 for (y = 0; y < h2; y++) {
104 yy = (int) (y*sy)*w1;
106 for (x = 0; x < w2; x++) {
107 xx = (int) (x*sx);
108 d = d1 + (yy+xx)*components;
110 for (c = 0; c < components; c++)
111 *d2++ = *d++;
116 #ifdef _WIN32
117 static int ExtSupported(const char *x) {
118 static const GLubyte *ext = NULL;
119 const char *c;
120 int xlen = strlen(x);
122 if (ext == NULL) ext = glGetString(GL_EXTENSIONS);
124 c = (const char*)ext;
126 while (*c != '\0') {
127 if (strcmp(c, x) == 0 && (c[xlen] == '\0' || c[xlen] == ' ')) return 1;
128 c++;
131 return 0;
133 #endif
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) {
138 int x, y, c;
139 int line = width*components;
141 if (width > 1 && height > 1) {
142 if (filter)
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;
147 data++;
149 data += components;
151 data += line;
153 else
154 for (y = 0; y < height; y += 2) {
155 for (x = 0; x < width; x += 2) {
156 for (c = 0; c < components; c++) {
157 *d++ = GET(0);
158 data++;
160 data += components;
162 data += line;
165 else if (width > 1 && height == 1) {
166 if (filter)
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;
171 data++;
173 data += components;
176 else
177 for (y = 0; y < height; y += 1) {
178 for (x = 0; x < width; x += 2) {
179 for (c = 0; c < components; c++) {
180 *d++ = GET(0);
181 data++;
183 data += components;
187 else if (width == 1 && height > 1) {
188 if (filter)
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;
193 data++;
196 data += line;
198 else
199 for (y = 0; y < height; y += 2) {
200 for (x = 0; x < width; x += 1) {
201 for (c = 0; c < components; c++) {
202 *d++ = GET(0);
203 data++;
206 data += line;
209 else {
210 return 0;
213 return 1;
216 #undef GET
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) {
220 int level = 0;
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);
225 level++;
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);
232 level++;
233 last = d;
236 free(d);
239 int APIENTRY pngLoadRaw(const char *filename, pngRawInfo *pinfo) {
240 int result;
241 FILE *fp = fopen(filename, "rb");
242 if (fp == NULL) return 0;
244 result = pngLoadRawF(fp, pinfo);
246 if (fclose(fp) != 0) {
247 if (result) {
248 free(pinfo->Data);
249 free(pinfo->Palette);
251 return 0;
254 return result;
257 int APIENTRY pngLoadRawF(FILE *fp, pngRawInfo *pinfo) {
258 unsigned char header[8];
259 png_structp png;
260 png_infop info;
261 png_infop endinfo;
262 png_bytep data;
263 png_bytep *row_p;
264 double fileGamma;
266 png_uint_32 width, height;
267 int depth, color;
269 png_uint_32 i;
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);
284 return 0;
286 // ~DH
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;
297 /*--GAMMA--*/
298 checkForGammaEnv();
299 if (png_get_gAMA(png, info, &fileGamma))
300 png_set_gamma(png, screenGamma, fileGamma);
301 else
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];
312 else
313 row_p[i] = &data[png_get_rowbytes(png, info)*i];
316 png_read_image(png, row_p);
317 free(row_p);
319 if (color == PNG_COLOR_TYPE_PALETTE) {
320 int cols;
321 png_get_PLTE(png, info, (png_colorp *) &pinfo->Palette, &cols);
323 else {
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;
330 else
331 pinfo->Components = 4;
332 pinfo->Alpha = 8;
334 else {
335 if (color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY)
336 pinfo->Components = 1;
337 else
338 pinfo->Components = 3;
339 pinfo->Alpha = 0;
342 pinfo->Data = data;
344 png_read_end(png, endinfo);
345 png_destroy_read_struct(&png, &info, &endinfo);
347 return 1;
350 int APIENTRY pngLoad(const char *filename, int mipmap, int trans, pngInfo *pinfo) {
351 int result;
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;
359 return result;
362 int APIENTRY pngLoadF(FILE *fp, int mipmap, int trans, pngInfo *pinfo) {
363 GLint pack, unpack;
364 unsigned char header[8];
365 png_structp png;
366 png_infop info;
367 png_infop endinfo;
368 png_bytep data, data2;
369 png_bytep *row_p;
370 double fileGamma;
372 png_uint_32 width, height, rw, rh;
373 int depth, color;
375 png_uint_32 i;
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);
388 return 0;
390 // ~DH
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);
397 if (pinfo != 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
407 #ifdef _WIN32
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;
418 #endif
419 #endif
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)
434 png_set_expand(png);
436 /*--GAMMA--*/
437 checkForGammaEnv();
438 if (png_get_gAMA(png, info, &fileGamma))
439 png_set_gamma(png, screenGamma, fileGamma);
440 else
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];
451 else
452 row_p[i] = &data[png_get_rowbytes(png, info)*i];
455 png_read_image(png, row_p);
456 free(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)
467 return 0;
469 Resize(channels, data, width, height, data2, rw, rh);
471 width = rw, height = rh;
472 free(data);
473 data = data2;
476 { /* OpenGL stuff */
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) {
484 png_colorp pal;
485 int cols;
486 GLint intf;
488 if (pinfo != NULL) pinfo->Alpha = 0;
489 png_get_PLTE(png, info, &pal, &cols);
491 switch (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;
498 default:
499 /*printf("Warning: Colour depth %i not recognised\n", cols);*/
500 return 0;
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);
505 else
506 #endif
507 if (trans == PNG_SOLID || trans == PNG_ALPHA || color == PNG_COLOR_TYPE_RGB_ALPHA || color == PNG_COLOR_TYPE_GRAY_ALPHA) {
508 GLenum glformat;
509 GLint glcomponent;
511 switch (color) {
512 case PNG_COLOR_TYPE_GRAY:
513 case PNG_COLOR_TYPE_RGB:
514 case PNG_COLOR_TYPE_PALETTE:
515 glformat = GL_RGB;
516 glcomponent = 3;
517 if (pinfo != NULL) pinfo->Alpha = 0;
518 break;
520 case PNG_COLOR_TYPE_GRAY_ALPHA:
521 case PNG_COLOR_TYPE_RGB_ALPHA:
522 glformat = GL_RGBA;
523 glcomponent = 4;
524 if (pinfo != NULL) pinfo->Alpha = 8;
525 break;
527 default:
528 /*puts("glformat not set");*/
529 return 0;
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);
536 else
537 glTexImage2D(GL_TEXTURE_2D, mipmap, glcomponent, width, height, 0, glformat, GL_UNSIGNED_BYTE, data);
539 else {
540 png_bytep p, endp, q;
541 int r, g, b, a;
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;
548 #define FORSTART \
549 do { \
550 r = *p++; /*red */ \
551 g = *p++; /*green*/ \
552 b = *p++; /*blue */ \
553 *q++ = r; \
554 *q++ = g; \
555 *q++ = b;
557 #define FOREND \
558 q++; \
559 } while (p != endp);
561 #define ALPHA *q
563 switch (trans) {
564 case PNG_CALLBACK:
565 FORSTART
566 ALPHA = AlphaCallback((unsigned char) r, (unsigned char) g, (unsigned char) b);
567 FOREND
568 break;
570 case PNG_STENCIL:
571 FORSTART
572 if (r == StencilRed && g == StencilGreen && b == StencilBlue)
573 ALPHA = 0;
574 else
575 ALPHA = 255;
576 FOREND
577 break;
579 case PNG_BLEND1:
580 FORSTART
581 a = r+g+b;
582 if (a > 255) ALPHA = 255; else ALPHA = a;
583 FOREND
584 break;
586 case PNG_BLEND2:
587 FORSTART
588 a = r+g+b;
589 if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
590 FOREND
591 break;
593 case PNG_BLEND3:
594 FORSTART
595 ALPHA = (r+g+b)/3;
596 FOREND
597 break;
599 case PNG_BLEND4:
600 FORSTART
601 a = r*r+g*g+b*b;
602 if (a > 255) ALPHA = 255; else ALPHA = a;
603 FOREND
604 break;
606 case PNG_BLEND5:
607 FORSTART
608 a = r*r+g*g+b*b;
609 if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
610 FOREND
611 break;
613 case PNG_BLEND6:
614 FORSTART
615 a = r*r+g*g+b*b;
616 if (a > 255*3) ALPHA = 255; else ALPHA = a/3;
617 FOREND
618 break;
620 case PNG_BLEND7:
621 FORSTART
622 a = r*r+g*g+b*b;
623 if (a > 255*255) ALPHA = 255; else ALPHA = (int) sqrt(a);
624 FOREND
625 break;
628 #undef FORSTART
629 #undef FOREND
630 #undef ALPHA
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);
636 else
637 glTexImage2D(GL_TEXTURE_2D, mipmap, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);
639 free(data2);
642 glPixelStorei(GL_PACK_ALIGNMENT, pack);
643 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
644 } /* OpenGL end */
646 png_read_end(png, endinfo);
647 png_destroy_read_struct(&png, &info, &endinfo);
649 free(data);
651 return 1;
654 static unsigned int SetParams(int wrapst, int magfilter, int minfilter) {
655 unsigned int id;
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);
666 return id;
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))
673 return id;
674 return 0;
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))
681 return id;
682 return 0;
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;
692 else
693 AlphaCallback = callback;
696 void APIENTRY pngSetViewingGamma(double viewingGamma) {
697 if(viewingGamma > 0) {
698 gammaExplicit = 1;
699 screenGamma = 2.2/viewingGamma;
701 else {
702 gammaExplicit = 0;
703 screenGamma = 2.2;
707 void APIENTRY pngSetStandardOrientation(int standardorientation) {
708 StandardOrientation = standardorientation;