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.
27
#ifdef _WIN32 /* Stupid Windows needs to include windows.h before gl.h */
32
/* Fabien Chereau 3-03-2004 */
33
#include "SDL_opengl.h"
40
/* Used to decide if GL/gl.h supports the paletted extension */
41
#ifdef GL_COLOR_INDEX1_EXT
42
/* Commented by Fabien Chereau 11/08/2002 */
43
/*#define SUPPORTS_PALETTE_EXT*/
46
static unsigned char DefaultAlphaCallback(unsigned char red, unsigned char green, unsigned char blue) {
50
static unsigned char StencilRed = 0, StencilGreen = 0, StencilBlue = 0;
51
static unsigned char (*AlphaCallback)(unsigned char red, unsigned char green, unsigned char blue) = DefaultAlphaCallback;
52
static int StandardOrientation = 0;
54
#ifdef SUPPORTS_PALETTE_EXT
56
static PFNGLCOLORTABLEEXTPROC glColorTableEXT = NULL;
60
static int PalettedTextures = -1;
61
static GLint MaxTextureSize = 0;
63
/* screenGamma = displayGamma/viewingGamma
64
* displayGamma = CRT has gamma of ~2.2
65
* viewingGamma depends on platform. PC is 1.0, Mac is 1.45, SGI defaults
66
* to 1.7, but this can be checked and changed w/ /usr/sbin/gamma command.
67
* If the environment variable VIEWING_GAMMA is set, adjust gamma per this value.
70
static double screenGamma = 2.2 / 1.45;
72
static double screenGamma = 2.2 / 1.7;
73
#else /* PC/default */
74
static double screenGamma = 2.2 / 1.0;
77
static char gammaExplicit = 0; /*if */
79
static void checkForGammaEnv()
82
char *gammaEnv = getenv("VIEWING_GAMMA");
84
if(gammaEnv && !gammaExplicit)
86
sscanf(gammaEnv, "%lf", &viewingGamma);
87
screenGamma = 2.2/viewingGamma;
91
/* Returns a safe texture size to use (ie a power of 2), based on the current texture size "i" */
92
static int SafeSize(int i) {
95
if (i > MaxTextureSize) return MaxTextureSize;
97
for (p = 0; p < 24; p++)
101
return MaxTextureSize;
104
/* Resize the texture since gluScaleImage doesn't work on everything */
105
static void Resize(int components, const png_bytep d1, int w1, int h1, png_bytep d2, int w2, int h2) {
106
const float sx = (float) w1/w2, sy = (float) h1/h2;
110
for (y = 0; y < h2; y++) {
111
yy = (int) (y*sy)*w1;
113
for (x = 0; x < w2; x++) {
115
d = d1 + (yy+xx)*components;
117
for (c = 0; c < components; c++)
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;
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];
271
png_uint_32 width, height;
276
if (pinfo == NULL) return 0;
278
fread(header, 1, 8, fp);
279
if (!png_check_sig(header, 8)) return 0;
281
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
282
info = png_create_info_struct(png);
283
endinfo = png_create_info_struct(png);
285
// DH: added following lines
286
if (setjmp(png->jmpbuf))
288
png_destroy_read_struct(&png, &info, &endinfo);
293
png_init_io(png, fp);
294
png_set_sig_bytes(png, 8);
295
png_read_info(png, info);
296
png_get_IHDR(png, info, &width, &height, &depth, &color, NULL, NULL, NULL);
298
pinfo->Width = width;
299
pinfo->Height = height;
300
pinfo->Depth = depth;
304
if (png_get_gAMA(png, info, &fileGamma))
305
png_set_gamma(png, screenGamma, fileGamma);
307
png_set_gamma(png, screenGamma, 1.0/2.2);
309
png_read_update_info(png, info);
311
data = (png_bytep) malloc(png_get_rowbytes(png, info)*height);
312
row_p = (png_bytep *) malloc(sizeof(png_bytep)*height);
314
for (i = 0; i < height; i++) {
315
if (StandardOrientation)
316
row_p[height - 1 - i] = &data[png_get_rowbytes(png, info)*i];
318
row_p[i] = &data[png_get_rowbytes(png, info)*i];
321
png_read_image(png, row_p);
324
if (color == PNG_COLOR_TYPE_PALETTE) {
326
png_get_PLTE(png, info, (png_colorp *) &pinfo->Palette, &cols);
329
pinfo->Palette = NULL;
332
if (color&PNG_COLOR_MASK_ALPHA) {
333
if (color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY_ALPHA)
334
pinfo->Components = 2;
336
pinfo->Components = 4;
340
if (color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY)
341
pinfo->Components = 1;
343
pinfo->Components = 3;
349
png_read_end(png, endinfo);
350
png_destroy_read_struct(&png, &info, &endinfo);
355
int APIENTRY pngLoad(const char *filename, int mipmap, int trans, pngInfo *pinfo) {
357
FILE *fp = fopen(filename, "rb");
358
if (fp == NULL) return 0;
360
result = pngLoadF(fp, mipmap, trans, pinfo);
362
if (fclose(fp) != 0) return 0;
367
int APIENTRY pngLoadF(FILE *fp, int mipmap, int trans, pngInfo *pinfo) {
369
unsigned char header[8];
373
png_bytep data, data2;
377
png_uint_32 width, height, rw, rh;
382
fread(header, 1, 8, fp);
383
if (!png_check_sig(header, 8)) return 0;
385
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
386
info = png_create_info_struct(png);
387
endinfo = png_create_info_struct(png);
389
// DH: added following lines
390
if (setjmp(png->jmpbuf))
392
png_destroy_read_struct(&png, &info, &endinfo);
397
png_init_io(png, fp);
398
png_set_sig_bytes(png, 8);
399
png_read_info(png, info);
400
png_get_IHDR(png, info, &width, &height, &depth, &color, NULL, NULL, NULL);
403
pinfo->Width = width;
404
pinfo->Height = height;
405
pinfo->Depth = depth;
408
if (MaxTextureSize == 0)
409
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &MaxTextureSize);
411
#ifdef SUPPORTS_PALETTE_EXT
413
if (PalettedTextures == -1)
414
PalettedTextures = ExtSupported("GL_EXT_paletted_texture") && (strstr((const char *) glGetString(GL_VERSION), "1.1.0 3Dfx Beta") == NULL);
416
if (PalettedTextures) {
417
if (glColorTableEXT == NULL) {
418
glColorTableEXT = (PFNGLCOLORTABLEEXTPROC) wglGetProcAddress("glColorTableEXT");
419
if (glColorTableEXT == NULL)
420
PalettedTextures = 0;
426
if (PalettedTextures == -1)
427
PalettedTextures = 0;
429
if (color == PNG_COLOR_TYPE_GRAY || color == PNG_COLOR_TYPE_GRAY_ALPHA)
430
png_set_gray_to_rgb(png);
432
if (color&PNG_COLOR_MASK_ALPHA && trans != PNG_ALPHA) {
433
png_set_strip_alpha(png);
434
color &= ~PNG_COLOR_MASK_ALPHA;
437
if (!(PalettedTextures && mipmap >= 0 && trans == PNG_SOLID))
438
if (color == PNG_COLOR_TYPE_PALETTE)
443
if (png_get_gAMA(png, info, &fileGamma))
444
png_set_gamma(png, screenGamma, fileGamma);
446
png_set_gamma(png, screenGamma, 1.0/2.2);
448
png_read_update_info(png, info);
450
data = (png_bytep) malloc(png_get_rowbytes(png, info)*height);
451
row_p = (png_bytep *) malloc(sizeof(png_bytep)*height);
453
for (i = 0; i < height; i++) {
454
if (StandardOrientation)
455
row_p[height - 1 - i] = &data[png_get_rowbytes(png, info)*i];
457
row_p[i] = &data[png_get_rowbytes(png, info)*i];
460
png_read_image(png, row_p);
463
rw = SafeSize(width), rh = SafeSize(height);
465
if (rw != width || rh != height) {
466
const int channels = png_get_rowbytes(png, info)/width;
468
data2 = (png_bytep) malloc(rw*rh*channels);
470
/* Doesn't work on certain sizes */
471
/* if (gluScaleImage(glformat, width, height, GL_UNSIGNED_BYTE, data, rw, rh, GL_UNSIGNED_BYTE, data2) != 0)
474
Resize(channels, data, width, height, data2, rw, rh);
476
width = rw, height = rh;
482
glGetIntegerv(GL_PACK_ALIGNMENT, &pack);
483
glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpack);
484
glPixelStorei(GL_PACK_ALIGNMENT, 1);
485
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
487
#ifdef SUPPORTS_PALETTE_EXT
488
if (PalettedTextures && mipmap >= 0 && trans == PNG_SOLID && color == PNG_COLOR_TYPE_PALETTE) {
493
if (pinfo != NULL) pinfo->Alpha = 0;
494
png_get_PLTE(png, info, &pal, &cols);
497
case 1<<1: intf = GL_COLOR_INDEX1_EXT; break;
498
case 1<<2: intf = GL_COLOR_INDEX2_EXT; break;
499
case 1<<4: intf = GL_COLOR_INDEX4_EXT; break;
500
case 1<<8: intf = GL_COLOR_INDEX8_EXT; break;
501
case 1<<12: intf = GL_COLOR_INDEX12_EXT; break;
502
case 1<<16: intf = GL_COLOR_INDEX16_EXT; break;
504
/*printf("Warning: Colour depth %i not recognised\n", cols);*/
507
glColorTableEXT(GL_TEXTURE_2D, GL_RGB8, cols, GL_RGB, GL_UNSIGNED_BYTE, pal);
508
glTexImage2D(GL_TEXTURE_2D, mipmap, intf, width, height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, data);
512
if (trans == PNG_SOLID || trans == PNG_ALPHA || color == PNG_COLOR_TYPE_RGB_ALPHA || color == PNG_COLOR_TYPE_GRAY_ALPHA) {
517
case PNG_COLOR_TYPE_GRAY:
518
case PNG_COLOR_TYPE_RGB:
519
case PNG_COLOR_TYPE_PALETTE:
522
if (pinfo != NULL) pinfo->Alpha = 0;
525
case PNG_COLOR_TYPE_GRAY_ALPHA:
526
case PNG_COLOR_TYPE_RGB_ALPHA:
529
if (pinfo != NULL) pinfo->Alpha = 8;
533
/*puts("glformat not set");*/
537
if (mipmap == PNG_BUILDMIPMAPS)
538
Build2DMipmaps(glcomponent, width, height, glformat, data, 1);
539
else if (mipmap == PNG_SIMPLEMIPMAPS)
540
Build2DMipmaps(glcomponent, width, height, glformat, data, 0);
542
glTexImage2D(GL_TEXTURE_2D, mipmap, glcomponent, width, height, 0, glformat, GL_UNSIGNED_BYTE, data);
545
png_bytep p, endp, q;
548
p = data, endp = p+width*height*3;
549
q = data2 = (png_bytep) malloc(sizeof(png_byte)*width*height*4);
551
if (pinfo != NULL) pinfo->Alpha = 8;
556
g = *p++; /*green*/ \
557
b = *p++; /*blue */ \
571
ALPHA = AlphaCallback((unsigned char) r, (unsigned char) g, (unsigned char) b);
577
if (r == StencilRed && g == StencilGreen && b == StencilBlue)
593
if (a > 255) ALPHA = 255; else ALPHA = a;
601
if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
614
if (a > 255) ALPHA = 255; else ALPHA = a;
621
if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
628
if (a > 255*3) ALPHA = 255; else ALPHA = a/3;
635
if (a > 255*255) ALPHA = 255; else ALPHA = (int) sqrt(a);
644
if (mipmap == PNG_BUILDMIPMAPS)
645
Build2DMipmaps(4, width, height, GL_RGBA, data2, 1);
646
else if (mipmap == PNG_SIMPLEMIPMAPS)
647
Build2DMipmaps(4, width, height, GL_RGBA, data2, 0);
649
glTexImage2D(GL_TEXTURE_2D, mipmap, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);
654
glPixelStorei(GL_PACK_ALIGNMENT, pack);
655
glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
658
png_read_end(png, endinfo);
659
png_destroy_read_struct(&png, &info, &endinfo);
666
static unsigned int SetParams(int wrapst, int magfilter, int minfilter) {
669
glGenTextures(1, &id);
670
glBindTexture(GL_TEXTURE_2D, id);
672
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapst);
673
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapst);
675
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter);
676
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter);
681
unsigned int APIENTRY pngBind(const char *filename, int mipmap, int trans, pngInfo *info, int wrapst, int minfilter, int magfilter) {
682
unsigned int id = SetParams(wrapst, magfilter, minfilter);
684
if (id != 0 && pngLoad(filename, mipmap, trans, info))
689
unsigned int APIENTRY pngBindF(FILE *file, int mipmap, int trans, pngInfo *info, int wrapst, int minfilter, int magfilter) {
690
unsigned int id = SetParams(wrapst, magfilter, minfilter);
691
//printf("coucou %d\n", file);
692
if (id != 0 && pngLoadF(file, mipmap, trans, info))
697
void APIENTRY pngSetStencil(unsigned char red, unsigned char green, unsigned char blue) {
698
StencilRed = red, StencilGreen = green, StencilBlue = blue;
701
void APIENTRY pngSetAlphaCallback(unsigned char (*callback)(unsigned char red, unsigned char green, unsigned char blue)) {
702
if (callback == NULL)
703
AlphaCallback = DefaultAlphaCallback;
705
AlphaCallback = callback;
708
void APIENTRY pngSetViewingGamma(double viewingGamma) {
709
if(viewingGamma > 0) {
711
screenGamma = 2.2/viewingGamma;
719
void APIENTRY pngSetStandardOrientation(int standardorientation) {
720
StandardOrientation = standardorientation;