| 1 | /* PNG loader library for OpenGL v1.45 (10/07/00) |
|---|
| 2 | * by Ben Wyatt ben@wyatt100.freeserve.co.uk |
|---|
| 3 | * Using LibPNG 1.0.2 and ZLib 1.1.3 |
|---|
| 4 | * |
|---|
| 5 | * This software is provided 'as-is', without any express or implied warranty. |
|---|
| 6 | * In no event will the author be held liable for any damages arising from the |
|---|
| 7 | * use of this software. |
|---|
| 8 | * |
|---|
| 9 | * Permission is hereby granted to use, copy, modify, and distribute this |
|---|
| 10 | * source code, or portions hereof, for any purpose, without fee, subject to |
|---|
| 11 | * the following restrictions: |
|---|
| 12 | * |
|---|
| 13 | * 1. The origin of this source code must not be misrepresented. You must not |
|---|
| 14 | * claim that you wrote the original software. If you use this software in |
|---|
| 15 | * a product, an acknowledgment in the product documentation would be |
|---|
| 16 | * appreciated but is not required. |
|---|
| 17 | * 2. Altered versions must be plainly marked as such and must not be |
|---|
| 18 | * misrepresented as being the original source. |
|---|
| 19 | * 3. This notice must not be removed or altered from any source distribution. |
|---|
| 20 | * |
|---|
| 21 | * --------------------------------------------------- |
|---|
| 22 | * This version has been modified for usage inside ARB |
|---|
| 23 | * http://arb-home.de/ |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | #ifdef _WIN32 // Stupid Windows needs to include windows.h before gl.h |
|---|
| 27 | #undef FAR |
|---|
| 28 | #include <windows.h> |
|---|
| 29 | #endif |
|---|
| 30 | |
|---|
| 31 | #include <GL/glew.h> |
|---|
| 32 | #include <GL/gl.h> |
|---|
| 33 | #include <GL/glext.h> |
|---|
| 34 | #include <GL/glpng.h> |
|---|
| 35 | #include <stdlib.h> |
|---|
| 36 | #include <math.h> |
|---|
| 37 | #include <png.h> |
|---|
| 38 | |
|---|
| 39 | #if 0 |
|---|
| 40 | #define GLPNG_CHECK_SIG(header,size) png_check_sig(header,size) // old libpng |
|---|
| 41 | #else |
|---|
| 42 | #define GLPNG_CHECK_SIG(header,size) (png_sig_cmp(header,0,size)==0) |
|---|
| 43 | #endif |
|---|
| 44 | |
|---|
| 45 | // Used to decide if GL/gl.h supports the paletted extension |
|---|
| 46 | #ifdef GL_COLOR_INDEX1_EXT |
|---|
| 47 | #define SUPPORTS_PALETTE_EXT |
|---|
| 48 | #endif |
|---|
| 49 | |
|---|
| 50 | #pragma GCC diagnostic ignored "-Wunused-parameter" |
|---|
| 51 | static unsigned char DefaultAlphaCallback(unsigned char red, unsigned char green, unsigned char blue) { |
|---|
| 52 | return 255; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | static unsigned char StencilRed = 0, StencilGreen = 0, StencilBlue = 0; |
|---|
| 56 | static unsigned char (*AlphaCallback)(unsigned char red, unsigned char green, unsigned char blue) = DefaultAlphaCallback; |
|---|
| 57 | static int StandardOrientation = 0; |
|---|
| 58 | |
|---|
| 59 | #ifdef SUPPORTS_PALETTE_EXT |
|---|
| 60 | #ifdef _WIN32 |
|---|
| 61 | static PFNGLCOLORTABLEEXTPROC glColorTableEXT = NULL; |
|---|
| 62 | #endif |
|---|
| 63 | #endif |
|---|
| 64 | |
|---|
| 65 | static int PalettedTextures = -1; |
|---|
| 66 | static GLint MaxTextureSize = 0; |
|---|
| 67 | |
|---|
| 68 | /* screenGamma = displayGamma/viewingGamma |
|---|
| 69 | * displayGamma = CRT has gamma of ~2.2 |
|---|
| 70 | * viewingGamma depends on platform. PC is 1.0, Mac is 1.45, |
|---|
| 71 | * but this can be checked and changed w/ /usr/sbin/gamma command. |
|---|
| 72 | * If the environment variable VIEWING_GAMMA is set, adjust gamma per this value. |
|---|
| 73 | */ |
|---|
| 74 | #ifdef _MAC |
|---|
| 75 | static double screenGamma = 2.2 / 1.45; |
|---|
| 76 | #else // PC/default |
|---|
| 77 | static double screenGamma = 2.2 / 1.0; |
|---|
| 78 | #endif |
|---|
| 79 | |
|---|
| 80 | static char gammaExplicit = 0; // if |
|---|
| 81 | |
|---|
| 82 | static void checkForGammaEnv() |
|---|
| 83 | { |
|---|
| 84 | double viewingGamma; |
|---|
| 85 | char *gammaEnv = getenv("VIEWING_GAMMA"); |
|---|
| 86 | |
|---|
| 87 | if (gammaEnv && !gammaExplicit) |
|---|
| 88 | { |
|---|
| 89 | sscanf(gammaEnv, "%lf", &viewingGamma); |
|---|
| 90 | screenGamma = 2.2/viewingGamma; |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | // Returns a safe texture size to use (ie a power of 2), based on the current texture size "i" |
|---|
| 95 | static int SafeSize(int i) { |
|---|
| 96 | int p; |
|---|
| 97 | |
|---|
| 98 | if (i > MaxTextureSize) return MaxTextureSize; |
|---|
| 99 | |
|---|
| 100 | for (p = 0; p < 24; p++) |
|---|
| 101 | if (i <= (1<<p)) |
|---|
| 102 | return 1<<p; |
|---|
| 103 | |
|---|
| 104 | return MaxTextureSize; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | // Resize the texture since gluScaleImage doesn't work on everything |
|---|
| 108 | static void Resize(int components, const png_bytep d1, int w1, int h1, png_bytep d2, int w2, int h2) { |
|---|
| 109 | const float sx = (float) w1/w2, sy = (float) h1/h2; |
|---|
| 110 | int x, y, xx, yy, c; |
|---|
| 111 | png_bytep d; |
|---|
| 112 | |
|---|
| 113 | for (y = 0; y < h2; y++) { |
|---|
| 114 | yy = (int) (y*sy)*w1; |
|---|
| 115 | |
|---|
| 116 | for (x = 0; x < w2; x++) { |
|---|
| 117 | xx = (int) (x*sx); |
|---|
| 118 | d = d1 + (yy+xx)*components; |
|---|
| 119 | |
|---|
| 120 | for (c = 0; c < components; c++) |
|---|
| 121 | *d2++ = *d++; |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | #ifdef SUPPORTS_PALETTE_EXT |
|---|
| 127 | #ifdef _WIN32 |
|---|
| 128 | static int ExtSupported(const char *x) { |
|---|
| 129 | static const GLubyte *ext = NULL; |
|---|
| 130 | const char *c; |
|---|
| 131 | int xlen = strlen(x); |
|---|
| 132 | |
|---|
| 133 | if (ext == NULL) ext = glGetString(GL_EXTENSIONS); |
|---|
| 134 | |
|---|
| 135 | c = (const char*)ext; |
|---|
| 136 | |
|---|
| 137 | while (*c != '\0') { |
|---|
| 138 | if (strcmp(c, x) == 0 && (c[xlen] == '\0' || c[xlen] == ' ')) return 1; |
|---|
| 139 | c++; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | return 0; |
|---|
| 143 | } |
|---|
| 144 | #endif |
|---|
| 145 | #endif |
|---|
| 146 | |
|---|
| 147 | #define GET(o) ((int)*(data + (o))) |
|---|
| 148 | |
|---|
| 149 | static int HalfSize(GLint components, GLint width, GLint height, const unsigned char *data, unsigned char *d, int filter) { |
|---|
| 150 | int x, y, c; |
|---|
| 151 | int line = width*components; |
|---|
| 152 | |
|---|
| 153 | if (width > 1 && height > 1) { |
|---|
| 154 | if (filter) |
|---|
| 155 | for (y = 0; y < height; y += 2) { |
|---|
| 156 | for (x = 0; x < width; x += 2) { |
|---|
| 157 | for (c = 0; c < components; c++) { |
|---|
| 158 | *d++ = (GET(0)+GET(components)+GET(line)+GET(line+components)) / 4; |
|---|
| 159 | data++; |
|---|
| 160 | } |
|---|
| 161 | data += components; |
|---|
| 162 | } |
|---|
| 163 | data += line; |
|---|
| 164 | } |
|---|
| 165 | else |
|---|
| 166 | for (y = 0; y < height; y += 2) { |
|---|
| 167 | for (x = 0; x < width; x += 2) { |
|---|
| 168 | for (c = 0; c < components; c++) { |
|---|
| 169 | *d++ = GET(0); |
|---|
| 170 | data++; |
|---|
| 171 | } |
|---|
| 172 | data += components; |
|---|
| 173 | } |
|---|
| 174 | data += line; |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | else if (width > 1 && height == 1) { |
|---|
| 178 | if (filter) |
|---|
| 179 | for (y = 0; y < height; y += 1) { |
|---|
| 180 | for (x = 0; x < width; x += 2) { |
|---|
| 181 | for (c = 0; c < components; c++) { |
|---|
| 182 | *d++ = (GET(0)+GET(components)) / 2; |
|---|
| 183 | data++; |
|---|
| 184 | } |
|---|
| 185 | data += components; |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | else |
|---|
| 189 | for (y = 0; y < height; y += 1) { |
|---|
| 190 | for (x = 0; x < width; x += 2) { |
|---|
| 191 | for (c = 0; c < components; c++) { |
|---|
| 192 | *d++ = GET(0); |
|---|
| 193 | data++; |
|---|
| 194 | } |
|---|
| 195 | data += components; |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | else if (width == 1 && height > 1) { |
|---|
| 200 | if (filter) |
|---|
| 201 | for (y = 0; y < height; y += 2) { |
|---|
| 202 | for (x = 0; x < width; x += 1) { |
|---|
| 203 | for (c = 0; c < components; c++) { |
|---|
| 204 | *d++ = (GET(0)+GET(line)) / 2; |
|---|
| 205 | data++; |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | data += line; |
|---|
| 209 | } |
|---|
| 210 | else |
|---|
| 211 | for (y = 0; y < height; y += 2) { |
|---|
| 212 | for (x = 0; x < width; x += 1) { |
|---|
| 213 | for (c = 0; c < components; c++) { |
|---|
| 214 | *d++ = GET(0); |
|---|
| 215 | data++; |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | data += line; |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | else { |
|---|
| 222 | return 0; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | return 1; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | #undef GET |
|---|
| 229 | |
|---|
| 230 | // Replacement for gluBuild2DMipmaps so GLU isn't needed |
|---|
| 231 | static void Build2DMipmaps(GLint components, GLint width, GLint height, GLenum format, const unsigned char *data, int filter) { |
|---|
| 232 | int level = 0; |
|---|
| 233 | unsigned char *d = (unsigned char *) malloc((width/2)*(height/2)*components+4); |
|---|
| 234 | const unsigned char *last = data; |
|---|
| 235 | |
|---|
| 236 | glTexImage2D(GL_TEXTURE_2D, level, components, width, height, 0, format, GL_UNSIGNED_BYTE, data); |
|---|
| 237 | level++; |
|---|
| 238 | |
|---|
| 239 | while (HalfSize(components, width, height, last, d, filter)) { |
|---|
| 240 | if (width > 1) width /= 2; |
|---|
| 241 | if (height > 1) height /= 2; |
|---|
| 242 | |
|---|
| 243 | glTexImage2D(GL_TEXTURE_2D, level, components, width, height, 0, format, GL_UNSIGNED_BYTE, d); |
|---|
| 244 | level++; |
|---|
| 245 | last = d; |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | free(d); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | static int APIENTRY pngLoadF(FILE *fp, int mipmap, int trans, pngInfo *pinfo) { |
|---|
| 252 | unsigned char header[8]; |
|---|
| 253 | |
|---|
| 254 | if (fread(header, 1, 8, fp) != 8) return 0; |
|---|
| 255 | if (!GLPNG_CHECK_SIG(header, 8)) return 0; |
|---|
| 256 | |
|---|
| 257 | GLint pack, unpack; |
|---|
| 258 | png_structp png; |
|---|
| 259 | png_infop info; |
|---|
| 260 | png_infop endinfo; |
|---|
| 261 | png_bytep data; |
|---|
| 262 | png_bytep *row_p; |
|---|
| 263 | double fileGamma; |
|---|
| 264 | |
|---|
| 265 | png_uint_32 width, height, rw, rh; |
|---|
| 266 | int depth, color; |
|---|
| 267 | |
|---|
| 268 | png_uint_32 i; |
|---|
| 269 | |
|---|
| 270 | png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
|---|
| 271 | info = png_create_info_struct(png); |
|---|
| 272 | endinfo = png_create_info_struct(png); |
|---|
| 273 | |
|---|
| 274 | // DH: added following lines |
|---|
| 275 | if (setjmp(png_jmpbuf(png))) { |
|---|
| 276 | png_destroy_read_struct(&png, &info, &endinfo); |
|---|
| 277 | return 0; |
|---|
| 278 | } |
|---|
| 279 | // ~DH |
|---|
| 280 | |
|---|
| 281 | png_init_io(png, fp); |
|---|
| 282 | png_set_sig_bytes(png, 8); |
|---|
| 283 | png_read_info(png, info); |
|---|
| 284 | png_get_IHDR(png, info, &width, &height, &depth, &color, NULL, NULL, NULL); |
|---|
| 285 | |
|---|
| 286 | if (pinfo != NULL) { |
|---|
| 287 | pinfo->Width = width; |
|---|
| 288 | pinfo->Height = height; |
|---|
| 289 | pinfo->Depth = depth; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | if (MaxTextureSize == 0) |
|---|
| 293 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &MaxTextureSize); |
|---|
| 294 | |
|---|
| 295 | #ifdef SUPPORTS_PALETTE_EXT |
|---|
| 296 | #ifdef _WIN32 |
|---|
| 297 | if (PalettedTextures == -1) |
|---|
| 298 | PalettedTextures = ExtSupported("GL_EXT_paletted_texture") && (strstr((const char *) glGetString(GL_VERSION), "1.1.0 3Dfx Beta") == NULL); |
|---|
| 299 | |
|---|
| 300 | if (PalettedTextures) { |
|---|
| 301 | if (glColorTableEXT == NULL) { |
|---|
| 302 | glColorTableEXT = (PFNGLCOLORTABLEEXTPROC) wglGetProcAddress("glColorTableEXT"); |
|---|
| 303 | if (glColorTableEXT == NULL) |
|---|
| 304 | PalettedTextures = 0; |
|---|
| 305 | } |
|---|
| 306 | } |
|---|
| 307 | #endif |
|---|
| 308 | #endif |
|---|
| 309 | |
|---|
| 310 | if (PalettedTextures == -1) |
|---|
| 311 | PalettedTextures = 0; |
|---|
| 312 | |
|---|
| 313 | if (color == PNG_COLOR_TYPE_GRAY || color == PNG_COLOR_TYPE_GRAY_ALPHA) |
|---|
| 314 | png_set_gray_to_rgb(png); |
|---|
| 315 | |
|---|
| 316 | if (color&PNG_COLOR_MASK_ALPHA && trans != GLPNG_ALPHA) { |
|---|
| 317 | png_set_strip_alpha(png); |
|---|
| 318 | color &= ~PNG_COLOR_MASK_ALPHA; |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | if (!(PalettedTextures && mipmap >= 0 && trans == GLPNG_SOLID)) |
|---|
| 322 | if (color == PNG_COLOR_TYPE_PALETTE) |
|---|
| 323 | png_set_expand(png); |
|---|
| 324 | |
|---|
| 325 | // --GAMMA-- |
|---|
| 326 | checkForGammaEnv(); |
|---|
| 327 | if (png_get_gAMA(png, info, &fileGamma)) |
|---|
| 328 | png_set_gamma(png, screenGamma, fileGamma); |
|---|
| 329 | else |
|---|
| 330 | png_set_gamma(png, screenGamma, 1.0/2.2); |
|---|
| 331 | |
|---|
| 332 | png_read_update_info(png, info); |
|---|
| 333 | |
|---|
| 334 | data = (png_bytep) malloc(png_get_rowbytes(png, info)*height); |
|---|
| 335 | row_p = (png_bytep *) malloc(sizeof(png_bytep)*height); |
|---|
| 336 | |
|---|
| 337 | for (i = 0; i < height; i++) { |
|---|
| 338 | if (StandardOrientation) |
|---|
| 339 | row_p[height - 1 - i] = &data[png_get_rowbytes(png, info)*i]; |
|---|
| 340 | else |
|---|
| 341 | row_p[i] = &data[png_get_rowbytes(png, info)*i]; |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | png_read_image(png, row_p); |
|---|
| 345 | free(row_p); |
|---|
| 346 | |
|---|
| 347 | rw = SafeSize(width), rh = SafeSize(height); |
|---|
| 348 | |
|---|
| 349 | if (rw != width || rh != height) { |
|---|
| 350 | const int channels = png_get_rowbytes(png, info)/width; |
|---|
| 351 | |
|---|
| 352 | png_bytep data2 = (png_bytep) malloc(rw*rh*channels); |
|---|
| 353 | |
|---|
| 354 | // Doesn't work on certain sizes |
|---|
| 355 | /* if (gluScaleImage(glformat, width, height, GL_UNSIGNED_BYTE, data, rw, rh, GL_UNSIGNED_BYTE, data2) != 0) |
|---|
| 356 | return 0; |
|---|
| 357 | */ |
|---|
| 358 | Resize(channels, data, width, height, data2, rw, rh); |
|---|
| 359 | |
|---|
| 360 | width = rw, height = rh; |
|---|
| 361 | free(data); |
|---|
| 362 | data = data2; |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | { // OpenGL stuff |
|---|
| 366 | glGetIntegerv(GL_PACK_ALIGNMENT, &pack); |
|---|
| 367 | glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpack); |
|---|
| 368 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
|---|
| 369 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
|---|
| 370 | |
|---|
| 371 | #ifdef SUPPORTS_PALETTE_EXT |
|---|
| 372 | if (PalettedTextures && mipmap >= 0 && trans == GLPNG_SOLID && color == PNG_COLOR_TYPE_PALETTE) { |
|---|
| 373 | png_colorp pal; |
|---|
| 374 | int cols; |
|---|
| 375 | GLint internalFormat; |
|---|
| 376 | |
|---|
| 377 | if (pinfo != NULL) pinfo->Alpha = 0; |
|---|
| 378 | png_get_PLTE(png, info, &pal, &cols); |
|---|
| 379 | |
|---|
| 380 | switch (cols) { |
|---|
| 381 | case 1<<1: internalFormat = GL_COLOR_INDEX1_EXT; break; |
|---|
| 382 | case 1<<2: internalFormat = GL_COLOR_INDEX2_EXT; break; |
|---|
| 383 | case 1<<4: internalFormat = GL_COLOR_INDEX4_EXT; break; |
|---|
| 384 | case 1<<8: internalFormat = GL_COLOR_INDEX8_EXT; break; |
|---|
| 385 | case 1<<12: internalFormat = GL_COLOR_INDEX12_EXT; break; |
|---|
| 386 | case 1<<16: internalFormat = GL_COLOR_INDEX16_EXT; break; |
|---|
| 387 | default: |
|---|
| 388 | return 0; |
|---|
| 389 | } |
|---|
| 390 | glColorTableEXT(GL_TEXTURE_2D, GL_RGB8, cols, GL_RGB, GL_UNSIGNED_BYTE, pal); |
|---|
| 391 | glTexImage2D(GL_TEXTURE_2D, mipmap, internalFormat, width, height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, data); |
|---|
| 392 | } |
|---|
| 393 | else |
|---|
| 394 | #endif |
|---|
| 395 | if (trans == GLPNG_SOLID || trans == GLPNG_ALPHA || color == PNG_COLOR_TYPE_RGB_ALPHA || color == PNG_COLOR_TYPE_GRAY_ALPHA) { |
|---|
| 396 | GLenum glformat; |
|---|
| 397 | GLint glcomponent; |
|---|
| 398 | |
|---|
| 399 | switch (color) { |
|---|
| 400 | case PNG_COLOR_TYPE_GRAY: |
|---|
| 401 | case PNG_COLOR_TYPE_RGB: |
|---|
| 402 | case PNG_COLOR_TYPE_PALETTE: |
|---|
| 403 | glformat = GL_RGB; |
|---|
| 404 | glcomponent = 3; |
|---|
| 405 | if (pinfo != NULL) pinfo->Alpha = 0; |
|---|
| 406 | break; |
|---|
| 407 | |
|---|
| 408 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
|---|
| 409 | case PNG_COLOR_TYPE_RGB_ALPHA: |
|---|
| 410 | glformat = GL_RGBA; |
|---|
| 411 | glcomponent = 4; |
|---|
| 412 | if (pinfo != NULL) pinfo->Alpha = 8; |
|---|
| 413 | break; |
|---|
| 414 | |
|---|
| 415 | default: |
|---|
| 416 | return 0; |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | if (mipmap == GLPNG_BUILDMIPMAPS) |
|---|
| 420 | Build2DMipmaps(glcomponent, width, height, glformat, data, 1); |
|---|
| 421 | else if (mipmap == GLPNG_SIMPLEMIPMAPS) |
|---|
| 422 | Build2DMipmaps(glcomponent, width, height, glformat, data, 0); |
|---|
| 423 | else |
|---|
| 424 | glTexImage2D(GL_TEXTURE_2D, mipmap, glcomponent, width, height, 0, glformat, GL_UNSIGNED_BYTE, data); |
|---|
| 425 | } |
|---|
| 426 | else { |
|---|
| 427 | png_bytep p, endp, q, data2; |
|---|
| 428 | int r, g, b, a; |
|---|
| 429 | |
|---|
| 430 | p = data, endp = p+width*height*3; |
|---|
| 431 | q = data2 = (png_bytep) malloc(sizeof(png_byte)*width*height*4); |
|---|
| 432 | |
|---|
| 433 | if (pinfo != NULL) pinfo->Alpha = 8; |
|---|
| 434 | |
|---|
| 435 | #define FORSTART \ |
|---|
| 436 | do { \ |
|---|
| 437 | r = *p++; /* red */ \ |
|---|
| 438 | g = *p++; /* green */ \ |
|---|
| 439 | b = *p++; /* blue */ \ |
|---|
| 440 | *q++ = r; \ |
|---|
| 441 | *q++ = g; \ |
|---|
| 442 | *q++ = b; |
|---|
| 443 | |
|---|
| 444 | #define FOREND \ |
|---|
| 445 | q++; \ |
|---|
| 446 | } while (p != endp); |
|---|
| 447 | |
|---|
| 448 | #define ALPHA *q |
|---|
| 449 | |
|---|
| 450 | switch (trans) { |
|---|
| 451 | case GLPNG_CALLBACK: |
|---|
| 452 | FORSTART |
|---|
| 453 | ALPHA = AlphaCallback((unsigned char) r, (unsigned char) g, (unsigned char) b); |
|---|
| 454 | FOREND |
|---|
| 455 | break; |
|---|
| 456 | |
|---|
| 457 | case GLPNG_STENCIL: |
|---|
| 458 | FORSTART |
|---|
| 459 | if (r == StencilRed && g == StencilGreen && b == StencilBlue) |
|---|
| 460 | ALPHA = 0; |
|---|
| 461 | else |
|---|
| 462 | ALPHA = 255; |
|---|
| 463 | FOREND |
|---|
| 464 | break; |
|---|
| 465 | |
|---|
| 466 | case GLPNG_BLEND1: |
|---|
| 467 | FORSTART |
|---|
| 468 | a = r+g+b; |
|---|
| 469 | if (a > 255) ALPHA = 255; else ALPHA = a; |
|---|
| 470 | FOREND |
|---|
| 471 | break; |
|---|
| 472 | |
|---|
| 473 | case GLPNG_BLEND2: |
|---|
| 474 | FORSTART |
|---|
| 475 | a = r+g+b; |
|---|
| 476 | if (a > 255*2) ALPHA = 255; else ALPHA = a/2; |
|---|
| 477 | FOREND |
|---|
| 478 | break; |
|---|
| 479 | |
|---|
| 480 | case GLPNG_BLEND3: |
|---|
| 481 | FORSTART |
|---|
| 482 | ALPHA = (r+g+b)/3; |
|---|
| 483 | FOREND |
|---|
| 484 | break; |
|---|
| 485 | |
|---|
| 486 | case GLPNG_BLEND4: |
|---|
| 487 | FORSTART |
|---|
| 488 | a = r*r+g*g+b*b; |
|---|
| 489 | if (a > 255) ALPHA = 255; else ALPHA = a; |
|---|
| 490 | FOREND |
|---|
| 491 | break; |
|---|
| 492 | |
|---|
| 493 | case GLPNG_BLEND5: |
|---|
| 494 | FORSTART |
|---|
| 495 | a = r*r+g*g+b*b; |
|---|
| 496 | if (a > 255*2) ALPHA = 255; else ALPHA = a/2; |
|---|
| 497 | FOREND |
|---|
| 498 | break; |
|---|
| 499 | |
|---|
| 500 | case GLPNG_BLEND6: |
|---|
| 501 | FORSTART |
|---|
| 502 | a = r*r+g*g+b*b; |
|---|
| 503 | if (a > 255*3) ALPHA = 255; else ALPHA = a/3; |
|---|
| 504 | FOREND |
|---|
| 505 | break; |
|---|
| 506 | |
|---|
| 507 | case GLPNG_BLEND7: |
|---|
| 508 | FORSTART |
|---|
| 509 | a = r*r+g*g+b*b; |
|---|
| 510 | if (a > 255*255) ALPHA = 255; else ALPHA = (int) sqrt(a); |
|---|
| 511 | FOREND |
|---|
| 512 | break; |
|---|
| 513 | } |
|---|
| 514 | |
|---|
| 515 | #undef FORSTART |
|---|
| 516 | #undef FOREND |
|---|
| 517 | #undef ALPHA |
|---|
| 518 | |
|---|
| 519 | if (mipmap == GLPNG_BUILDMIPMAPS) |
|---|
| 520 | Build2DMipmaps(4, width, height, GL_RGBA, data2, 1); |
|---|
| 521 | else if (mipmap == GLPNG_SIMPLEMIPMAPS) |
|---|
| 522 | Build2DMipmaps(4, width, height, GL_RGBA, data2, 0); |
|---|
| 523 | else |
|---|
| 524 | glTexImage2D(GL_TEXTURE_2D, mipmap, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2); |
|---|
| 525 | |
|---|
| 526 | free(data2); |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | glPixelStorei(GL_PACK_ALIGNMENT, pack); |
|---|
| 530 | glPixelStorei(GL_UNPACK_ALIGNMENT, unpack); |
|---|
| 531 | } // OpenGL end |
|---|
| 532 | |
|---|
| 533 | png_read_end(png, endinfo); |
|---|
| 534 | png_destroy_read_struct(&png, &info, &endinfo); |
|---|
| 535 | |
|---|
| 536 | free(data); |
|---|
| 537 | |
|---|
| 538 | return 1; |
|---|
| 539 | } |
|---|
| 540 | |
|---|
| 541 | static int APIENTRY pngLoad(const char *filename, int mipmap, int trans, pngInfo *pinfo) { |
|---|
| 542 | int result; |
|---|
| 543 | FILE *fp = fopen(filename, "rb"); |
|---|
| 544 | if (fp == NULL) return 0; |
|---|
| 545 | |
|---|
| 546 | result = pngLoadF(fp, mipmap, trans, pinfo); |
|---|
| 547 | |
|---|
| 548 | if (fclose(fp) != 0) return 0; |
|---|
| 549 | |
|---|
| 550 | return result; |
|---|
| 551 | } |
|---|
| 552 | |
|---|
| 553 | static unsigned int SetParams(int wrapst, int magfilter, int minfilter) { |
|---|
| 554 | unsigned int id; |
|---|
| 555 | |
|---|
| 556 | glGenTextures(1, &id); |
|---|
| 557 | glBindTexture(GL_TEXTURE_2D, id); |
|---|
| 558 | |
|---|
| 559 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapst); |
|---|
| 560 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapst); |
|---|
| 561 | |
|---|
| 562 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter); |
|---|
| 563 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter); |
|---|
| 564 | |
|---|
| 565 | return id; |
|---|
| 566 | } |
|---|
| 567 | |
|---|
| 568 | unsigned int APIENTRY pngBind(const char *filename, int mipmap, int trans, pngInfo *info, int wrapst, int minfilter, int magfilter) { |
|---|
| 569 | unsigned int id = SetParams(wrapst, magfilter, minfilter); |
|---|
| 570 | |
|---|
| 571 | if (id != 0 && pngLoad(filename, mipmap, trans, info)) |
|---|
| 572 | return id; |
|---|
| 573 | return 0; |
|---|
| 574 | } |
|---|
| 575 | |
|---|