~ubuntu-branches/ubuntu/precise/stellarium/precise

« back to all changes in this revision

Viewing changes to src/glpng/glpng.c

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2008-05-19 21:28:23 UTC
  • mfrom: (3.1.5 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080519212823-m5nfiuntxstxzxj7
Tags: 0.9.1-4
Add libxcursor-dev, libxfixes-dev, libxinerama-dev, libqt4-opengl-dev to
build-deps (Closes: #479906)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
5
 
 *
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.
9
 
 *
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:
13
 
 *
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.
21
 
 */
22
 
 
23
 
#ifdef HAVE_CONFIG_H
24
 
# include "config.h"
25
 
#endif
26
 
 
27
 
#ifdef _WIN32 /* Stupid Windows needs to include windows.h before gl.h */
28
 
        #undef FAR
29
 
        #include <windows.h>
30
 
#endif
31
 
 
32
 
/* Fabien Chereau 3-03-2004 */
33
 
#include "SDL_opengl.h"
34
 
 
35
 
#include "glpng.h"
36
 
#include <stdlib.h>
37
 
#include <math.h>
38
 
#include "png.h"
39
 
 
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*/
44
 
#endif
45
 
 
46
 
static unsigned char DefaultAlphaCallback(unsigned char red, unsigned char green, unsigned char blue) {
47
 
        return 255;
48
 
}
49
 
 
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;
53
 
 
54
 
#ifdef SUPPORTS_PALETTE_EXT
55
 
#ifdef _WIN32
56
 
static PFNGLCOLORTABLEEXTPROC glColorTableEXT = NULL;
57
 
#endif
58
 
#endif
59
 
 
60
 
static int PalettedTextures = -1;
61
 
static GLint MaxTextureSize = 0;
62
 
 
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.
68
 
 */
69
 
#ifdef _MAC
70
 
        static double screenGamma = 2.2 / 1.45;
71
 
#elif SGI
72
 
        static double screenGamma = 2.2 / 1.7;
73
 
#else /* PC/default */
74
 
        static double screenGamma = 2.2 / 1.0;
75
 
#endif
76
 
 
77
 
static char gammaExplicit = 0;  /*if  */
78
 
 
79
 
static void checkForGammaEnv()
80
 
{
81
 
        double viewingGamma;
82
 
        char *gammaEnv = getenv("VIEWING_GAMMA");
83
 
 
84
 
        if(gammaEnv && !gammaExplicit)
85
 
        {
86
 
                sscanf(gammaEnv, "%lf", &viewingGamma);
87
 
                screenGamma = 2.2/viewingGamma;
88
 
        }
89
 
}
90
 
 
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) {
93
 
        int p;
94
 
 
95
 
        if (i > MaxTextureSize) return MaxTextureSize;
96
 
 
97
 
        for (p = 0; p < 24; p++)
98
 
                if (i <= (1<<p))
99
 
                        return 1<<p;
100
 
 
101
 
        return MaxTextureSize;
102
 
}
103
 
 
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;
107
 
        int x, y, xx, yy, c;
108
 
        png_bytep d;
109
 
 
110
 
        for (y = 0; y < h2; y++) {
111
 
                yy = (int) (y*sy)*w1;
112
 
 
113
 
                for (x = 0; x < w2; x++) {
114
 
                        xx = (int) (x*sx);
115
 
                        d = d1 + (yy+xx)*components;
116
 
 
117
 
                        for (c = 0; c < components; c++)
118
 
                                *d2++ = *d++;
119
 
                }
120
 
        }
121
 
}
122
 
/*
123
 
static int ExtSupported(const char *x) {
124
 
        static const GLubyte *ext = NULL;
125
 
        const char *c;
126
 
        int xlen = strlen(x);
127
 
 
128
 
        if (ext == NULL) ext = glGetString(GL_EXTENSIONS);
129
 
 
130
 
        c = (const char*)ext;
131
 
 
132
 
        while (*c != '\0') {
133
 
                if (strcmp(c, x) == 0 && (c[xlen] == '\0' || c[xlen] == ' ')) return 1;
134
 
                c++;
135
 
        }
136
 
 
137
 
        return 0;
138
 
}
139
 
*/
140
 
#define GET(o) ((int)*(data + (o)))
141
 
 
142
 
static int HalfSize(GLint components, GLint width, GLint height, const unsigned char *data, unsigned char *d, int filter) {
143
 
        int x, y, c;
144
 
        int line = width*components;
145
 
 
146
 
        if (width > 1 && height > 1) {
147
 
                if (filter)
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;
152
 
                                                data++;
153
 
                                        }
154
 
                                        data += components;
155
 
                                }
156
 
                                data += line;
157
 
                        }
158
 
                else
159
 
                        for (y = 0; y < height; y += 2) {
160
 
                                for (x = 0; x < width; x += 2) {
161
 
                                        for (c = 0; c < components; c++) {
162
 
                                                *d++ = GET(0);
163
 
                                                data++;
164
 
                                        }
165
 
                                        data += components;
166
 
                                }
167
 
                                data += line;
168
 
                        }
169
 
        }
170
 
        else if (width > 1 && height == 1) {
171
 
                if (filter)
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;
176
 
                                                data++;
177
 
                                        }
178
 
                                        data += components;
179
 
                                }
180
 
                        }
181
 
                else
182
 
                        for (y = 0; y < height; y += 1) {
183
 
                                for (x = 0; x < width; x += 2) {
184
 
                                        for (c = 0; c < components; c++) {
185
 
                                                *d++ = GET(0);
186
 
                                                data++;
187
 
                                        }
188
 
                                        data += components;
189
 
                                }
190
 
                        }
191
 
        }
192
 
        else if (width == 1 && height > 1) {
193
 
                if (filter)
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;
198
 
                                                data++;
199
 
                                        }
200
 
                                }
201
 
                                data += line;
202
 
                        }
203
 
                else
204
 
                        for (y = 0; y < height; y += 2) {
205
 
                                for (x = 0; x < width; x += 1) {
206
 
                                        for (c = 0; c < components; c++) {
207
 
                                                *d++ = GET(0);
208
 
                                                data++;
209
 
                                        }
210
 
                                }
211
 
                                data += line;
212
 
                        }
213
 
        }
214
 
        else {
215
 
                return 0;
216
 
        }
217
 
 
218
 
        return 1;
219
 
}
220
 
 
221
 
#undef GET
222
 
 
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) {
225
 
        int level = 0;
226
 
        unsigned char *d = (unsigned char *) malloc((width/2)*(height/2)*components+4);
227
 
        const unsigned char *last = data;
228
 
 
229
 
        glTexImage2D(GL_TEXTURE_2D, level, components, width, height, 0, format, GL_UNSIGNED_BYTE, data);
230
 
        level++;
231
 
 
232
 
        while (HalfSize(components, width, height, last, d, filter)) {
233
 
                if (width  > 1) width  /= 2;
234
 
                if (height > 1) height /= 2;
235
 
 
236
 
                glTexImage2D(GL_TEXTURE_2D, level, components, width, height, 0, format, GL_UNSIGNED_BYTE, d);
237
 
                level++;
238
 
                last = d;
239
 
        }
240
 
 
241
 
        free(d);
242
 
}
243
 
 
244
 
int APIENTRY pngLoadRaw(const char *filename, pngRawInfo *pinfo) {
245
 
        int result;
246
 
        FILE *fp = fopen(filename, "rb");
247
 
        if (fp == NULL) return 0;
248
 
 
249
 
        result = pngLoadRawF(fp, pinfo);
250
 
 
251
 
        if (fclose(fp) != 0) {
252
 
                if (result) {
253
 
                        free(pinfo->Data);
254
 
                        free(pinfo->Palette);
255
 
                }
256
 
                return 0;
257
 
        }
258
 
 
259
 
        return result;
260
 
}
261
 
 
262
 
int APIENTRY pngLoadRawF(FILE *fp, pngRawInfo *pinfo) {
263
 
        unsigned char header[8];
264
 
        png_structp png;
265
 
        png_infop   info;
266
 
        png_infop   endinfo;
267
 
        png_bytep   data;
268
 
   png_bytep  *row_p;
269
 
   double       fileGamma;
270
 
 
271
 
        png_uint_32 width, height;
272
 
        int depth, color;
273
 
 
274
 
        png_uint_32 i;
275
 
 
276
 
        if (pinfo == NULL) return 0;
277
 
 
278
 
        fread(header, 1, 8, fp);
279
 
        if (!png_check_sig(header, 8)) return 0;
280
 
 
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);
284
 
 
285
 
        // DH: added following lines
286
 
        if (setjmp(png->jmpbuf))
287
 
        {
288
 
                png_destroy_read_struct(&png, &info, &endinfo);
289
 
                return 0;
290
 
        }
291
 
        // ~DH
292
 
 
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);
297
 
 
298
 
        pinfo->Width  = width;
299
 
        pinfo->Height = height;
300
 
        pinfo->Depth  = depth;
301
 
 
302
 
        /*--GAMMA--*/
303
 
        checkForGammaEnv();
304
 
        if (png_get_gAMA(png, info, &fileGamma))
305
 
                png_set_gamma(png, screenGamma, fileGamma);
306
 
        else
307
 
                png_set_gamma(png, screenGamma, 1.0/2.2);
308
 
 
309
 
        png_read_update_info(png, info);
310
 
 
311
 
        data = (png_bytep) malloc(png_get_rowbytes(png, info)*height);
312
 
        row_p = (png_bytep *) malloc(sizeof(png_bytep)*height);
313
 
 
314
 
        for (i = 0; i < height; i++) {
315
 
                if (StandardOrientation)
316
 
                        row_p[height - 1 - i] = &data[png_get_rowbytes(png, info)*i];
317
 
                else
318
 
                        row_p[i] = &data[png_get_rowbytes(png, info)*i];
319
 
        }
320
 
 
321
 
        png_read_image(png, row_p);
322
 
        free(row_p);
323
 
 
324
 
        if (color == PNG_COLOR_TYPE_PALETTE) {
325
 
                int cols;
326
 
                png_get_PLTE(png, info, (png_colorp *) &pinfo->Palette, &cols);
327
 
        }
328
 
        else {
329
 
                pinfo->Palette = NULL;
330
 
        }
331
 
 
332
 
        if (color&PNG_COLOR_MASK_ALPHA) {
333
 
                if (color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY_ALPHA)
334
 
                        pinfo->Components = 2;
335
 
                else
336
 
                        pinfo->Components = 4;
337
 
                pinfo->Alpha = 8;
338
 
        }
339
 
        else {
340
 
                if (color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY)
341
 
                        pinfo->Components = 1;
342
 
                else
343
 
                        pinfo->Components = 3;
344
 
                pinfo->Alpha = 0;
345
 
        }
346
 
 
347
 
        pinfo->Data = data;
348
 
 
349
 
   png_read_end(png, endinfo);
350
 
        png_destroy_read_struct(&png, &info, &endinfo);
351
 
 
352
 
        return 1;
353
 
}
354
 
 
355
 
int APIENTRY pngLoad(const char *filename, int mipmap, int trans, pngInfo *pinfo) {
356
 
        int result;
357
 
        FILE *fp = fopen(filename, "rb");
358
 
        if (fp == NULL) return 0;
359
 
 
360
 
        result = pngLoadF(fp, mipmap, trans, pinfo);
361
 
 
362
 
        if (fclose(fp) != 0) return 0;
363
 
 
364
 
        return result;
365
 
}
366
 
 
367
 
int APIENTRY pngLoadF(FILE *fp, int mipmap, int trans, pngInfo *pinfo) {
368
 
        GLint pack, unpack;
369
 
        unsigned char header[8];
370
 
        png_structp png;
371
 
        png_infop   info;
372
 
        png_infop   endinfo;
373
 
        png_bytep   data, data2;
374
 
   png_bytep  *row_p;
375
 
   double       fileGamma;
376
 
 
377
 
        png_uint_32 width, height, rw, rh;
378
 
        int depth, color;
379
 
 
380
 
        png_uint_32 i;
381
 
 
382
 
        fread(header, 1, 8, fp);
383
 
        if (!png_check_sig(header, 8)) return 0;
384
 
 
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);
388
 
 
389
 
        // DH: added following lines
390
 
        if (setjmp(png->jmpbuf))
391
 
        {
392
 
                png_destroy_read_struct(&png, &info, &endinfo);
393
 
                return 0;
394
 
        }
395
 
        // ~DH
396
 
 
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);
401
 
 
402
 
        if (pinfo != NULL) {
403
 
                pinfo->Width  = width;
404
 
                pinfo->Height = height;
405
 
                pinfo->Depth  = depth;
406
 
        }
407
 
 
408
 
        if (MaxTextureSize == 0)
409
 
                glGetIntegerv(GL_MAX_TEXTURE_SIZE, &MaxTextureSize);
410
 
 
411
 
        #ifdef SUPPORTS_PALETTE_EXT
412
 
        #ifdef _WIN32
413
 
                if (PalettedTextures == -1)
414
 
                        PalettedTextures = ExtSupported("GL_EXT_paletted_texture") && (strstr((const char *) glGetString(GL_VERSION), "1.1.0 3Dfx Beta") == NULL);
415
 
 
416
 
                if (PalettedTextures) {
417
 
                        if (glColorTableEXT == NULL) {
418
 
                                glColorTableEXT = (PFNGLCOLORTABLEEXTPROC) wglGetProcAddress("glColorTableEXT");
419
 
                                if (glColorTableEXT == NULL)
420
 
                                        PalettedTextures = 0;
421
 
                        }
422
 
                }
423
 
        #endif
424
 
        #endif
425
 
 
426
 
        if (PalettedTextures == -1)
427
 
                PalettedTextures = 0;
428
 
 
429
 
        if (color == PNG_COLOR_TYPE_GRAY || color == PNG_COLOR_TYPE_GRAY_ALPHA)
430
 
                png_set_gray_to_rgb(png);
431
 
 
432
 
        if (color&PNG_COLOR_MASK_ALPHA && trans != PNG_ALPHA) {
433
 
                png_set_strip_alpha(png);
434
 
                color &= ~PNG_COLOR_MASK_ALPHA;
435
 
        }
436
 
 
437
 
        if (!(PalettedTextures && mipmap >= 0 && trans == PNG_SOLID))
438
 
                if (color == PNG_COLOR_TYPE_PALETTE)
439
 
                        png_set_expand(png);
440
 
 
441
 
        /*--GAMMA--*/
442
 
        checkForGammaEnv();
443
 
        if (png_get_gAMA(png, info, &fileGamma))
444
 
                png_set_gamma(png, screenGamma, fileGamma);
445
 
        else
446
 
                png_set_gamma(png, screenGamma, 1.0/2.2);
447
 
 
448
 
        png_read_update_info(png, info);
449
 
 
450
 
        data = (png_bytep) malloc(png_get_rowbytes(png, info)*height);
451
 
        row_p = (png_bytep *) malloc(sizeof(png_bytep)*height);
452
 
 
453
 
        for (i = 0; i < height; i++) {
454
 
                if (StandardOrientation)
455
 
                        row_p[height - 1 - i] = &data[png_get_rowbytes(png, info)*i];
456
 
                else
457
 
                        row_p[i] = &data[png_get_rowbytes(png, info)*i];
458
 
        }
459
 
 
460
 
        png_read_image(png, row_p);
461
 
        free(row_p);
462
 
 
463
 
        rw = SafeSize(width), rh = SafeSize(height);
464
 
 
465
 
        if (rw != width || rh != height) {
466
 
                const int channels = png_get_rowbytes(png, info)/width;
467
 
 
468
 
                data2 = (png_bytep) malloc(rw*rh*channels);
469
 
 
470
 
                /* Doesn't work on certain sizes */
471
 
/*              if (gluScaleImage(glformat, width, height, GL_UNSIGNED_BYTE, data, rw, rh, GL_UNSIGNED_BYTE, data2) != 0)
472
 
                        return 0;
473
 
*/
474
 
                Resize(channels, data, width, height, data2, rw, rh);
475
 
 
476
 
                width = rw, height = rh;
477
 
                free(data);
478
 
                data = data2;
479
 
        }
480
 
 
481
 
        { /* OpenGL stuff */
482
 
                glGetIntegerv(GL_PACK_ALIGNMENT, &pack);
483
 
                glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpack);
484
 
                glPixelStorei(GL_PACK_ALIGNMENT, 1);
485
 
                glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
486
 
 
487
 
                #ifdef SUPPORTS_PALETTE_EXT
488
 
                if (PalettedTextures && mipmap >= 0 && trans == PNG_SOLID && color == PNG_COLOR_TYPE_PALETTE) {
489
 
                        png_colorp pal;
490
 
                        int cols;
491
 
                        GLint intf;
492
 
 
493
 
                        if (pinfo != NULL) pinfo->Alpha = 0;
494
 
                        png_get_PLTE(png, info, &pal, &cols);
495
 
 
496
 
                        switch (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;
503
 
                                default:
504
 
                                        /*printf("Warning: Colour depth %i not recognised\n", cols);*/
505
 
                                        return 0;
506
 
                        }
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);
509
 
                }
510
 
                else
511
 
                #endif
512
 
                if (trans == PNG_SOLID || trans == PNG_ALPHA || color == PNG_COLOR_TYPE_RGB_ALPHA || color == PNG_COLOR_TYPE_GRAY_ALPHA) {
513
 
                        GLenum glformat;
514
 
                        GLint glcomponent;
515
 
 
516
 
                        switch (color) {
517
 
                                case PNG_COLOR_TYPE_GRAY:
518
 
                                case PNG_COLOR_TYPE_RGB:
519
 
                                case PNG_COLOR_TYPE_PALETTE:
520
 
                                        glformat = GL_RGB;
521
 
                                        glcomponent = 3;
522
 
                                        if (pinfo != NULL) pinfo->Alpha = 0;
523
 
                                        break;
524
 
 
525
 
                                case PNG_COLOR_TYPE_GRAY_ALPHA:
526
 
                                case PNG_COLOR_TYPE_RGB_ALPHA:
527
 
                                        glformat = GL_RGBA;
528
 
                                        glcomponent = 4;
529
 
                                        if (pinfo != NULL) pinfo->Alpha = 8;
530
 
                                        break;
531
 
 
532
 
                                default:
533
 
                                        /*puts("glformat not set");*/
534
 
                                        return 0;
535
 
                        }
536
 
 
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);
541
 
                        else
542
 
                                glTexImage2D(GL_TEXTURE_2D, mipmap, glcomponent, width, height, 0, glformat, GL_UNSIGNED_BYTE, data);
543
 
                }
544
 
                else {
545
 
                        png_bytep p, endp, q;
546
 
                        int r, g, b, a;
547
 
 
548
 
                        p = data, endp = p+width*height*3;
549
 
                        q = data2 = (png_bytep) malloc(sizeof(png_byte)*width*height*4);
550
 
 
551
 
                        if (pinfo != NULL) pinfo->Alpha = 8;
552
 
 
553
 
                        #define FORSTART \
554
 
                                do { \
555
 
                                        r = *p++; /*red  */ \
556
 
                                        g = *p++; /*green*/ \
557
 
                                        b = *p++; /*blue */ \
558
 
                                        *q++ = r; \
559
 
                                        *q++ = g; \
560
 
                                        *q++ = b;
561
 
 
562
 
                        #define FOREND \
563
 
                                        q++; \
564
 
                                } while (p != endp);
565
 
 
566
 
                        #define ALPHA *q
567
 
 
568
 
                        switch (trans) {
569
 
                                case PNG_CALLBACK:
570
 
                                        FORSTART
571
 
                                                ALPHA = AlphaCallback((unsigned char) r, (unsigned char) g, (unsigned char) b);
572
 
                                        FOREND
573
 
                                        break;
574
 
 
575
 
                                case PNG_STENCIL:
576
 
                                        FORSTART
577
 
                                                if (r == StencilRed && g == StencilGreen && b == StencilBlue)
578
 
                                                        ALPHA = 0;
579
 
                                                else
580
 
                                                        ALPHA = 255;
581
 
                                        FOREND
582
 
                                        break;
583
 
 
584
 
                                case PNG_BLEND1:
585
 
                                        do { 
586
 
                                                r = *p++; /*red  */ 
587
 
                                                g = *p++; /*green*/ 
588
 
                                                b = *p++; /*blue */ 
589
 
                                                *q++ = r; 
590
 
                                                *q++ = g; 
591
 
                                                *q++ = b;
592
 
                                                a = r+g+b;
593
 
                                                if (a > 255) ALPHA = 255; else ALPHA = a;
594
 
                                                q++;
595
 
                                        } while (p != endp);
596
 
                                        break;
597
 
 
598
 
                                case PNG_BLEND2:
599
 
                                        FORSTART
600
 
                                                a = r+g+b;
601
 
                                                if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
602
 
                                        FOREND
603
 
                                        break;
604
 
 
605
 
                                case PNG_BLEND3:
606
 
                                        FORSTART
607
 
                                                ALPHA = (r+g+b)/3;
608
 
                                        FOREND
609
 
                                        break;
610
 
 
611
 
                                case PNG_BLEND4:
612
 
                                        FORSTART
613
 
                                                a = r*r+g*g+b*b;
614
 
                                                if (a > 255) ALPHA = 255; else ALPHA = a;
615
 
                                        FOREND
616
 
                                        break;
617
 
 
618
 
                                case PNG_BLEND5:
619
 
                                        FORSTART
620
 
                                                a = r*r+g*g+b*b;
621
 
                                                if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
622
 
                                        FOREND
623
 
                                        break;
624
 
 
625
 
                                case PNG_BLEND6:
626
 
                                        FORSTART
627
 
                                                a = r*r+g*g+b*b;
628
 
                                                if (a > 255*3) ALPHA = 255; else ALPHA = a/3;
629
 
                                        FOREND
630
 
                                        break;
631
 
 
632
 
                                case PNG_BLEND7:
633
 
                                        FORSTART
634
 
                                                a = r*r+g*g+b*b;
635
 
                                                if (a > 255*255) ALPHA = 255; else ALPHA = (int) sqrt(a);
636
 
                                        FOREND
637
 
                                        break;
638
 
                        }
639
 
 
640
 
                        #undef FORSTART
641
 
                        #undef FOREND
642
 
                        #undef ALPHA
643
 
 
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);
648
 
                        else
649
 
                                glTexImage2D(GL_TEXTURE_2D, mipmap, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);
650
 
 
651
 
                        free(data2);
652
 
                }
653
 
 
654
 
                glPixelStorei(GL_PACK_ALIGNMENT, pack);
655
 
                glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
656
 
        } /* OpenGL end */
657
 
 
658
 
   png_read_end(png, endinfo);
659
 
        png_destroy_read_struct(&png, &info, &endinfo);
660
 
 
661
 
        free(data);
662
 
 
663
 
        return 1;
664
 
}
665
 
 
666
 
static unsigned int SetParams(int wrapst, int magfilter, int minfilter) {
667
 
        unsigned int id;
668
 
 
669
 
        glGenTextures(1, &id);
670
 
        glBindTexture(GL_TEXTURE_2D, id);
671
 
 
672
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapst);
673
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapst);
674
 
 
675
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter);
676
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter);
677
 
 
678
 
        return id;
679
 
}
680
 
 
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);
683
 
 
684
 
        if (id != 0 && pngLoad(filename, mipmap, trans, info))
685
 
                return id;
686
 
        return 0;
687
 
}
688
 
 
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))
693
 
                return id;
694
 
        return 0;
695
 
}
696
 
 
697
 
void APIENTRY pngSetStencil(unsigned char red, unsigned char green, unsigned char blue) {
698
 
        StencilRed = red, StencilGreen = green, StencilBlue = blue;
699
 
}
700
 
 
701
 
void APIENTRY pngSetAlphaCallback(unsigned char (*callback)(unsigned char red, unsigned char green, unsigned char blue)) {
702
 
        if (callback == NULL)
703
 
                AlphaCallback = DefaultAlphaCallback;
704
 
        else
705
 
                AlphaCallback = callback;
706
 
}
707
 
 
708
 
void APIENTRY pngSetViewingGamma(double viewingGamma) {
709
 
        if(viewingGamma > 0) {
710
 
                gammaExplicit = 1;
711
 
                screenGamma = 2.2/viewingGamma;
712
 
        }
713
 
        else {
714
 
                gammaExplicit = 0;
715
 
                screenGamma = 2.2;
716
 
        }
717
 
}
718
 
 
719
 
void APIENTRY pngSetStandardOrientation(int standardorientation) {
720
 
        StandardOrientation = standardorientation;
721
 
}