~ubuntu-branches/ubuntu/trusty/teeworlds/trusty-updates

« back to all changes in this revision

Viewing changes to src/engine/external/glfw/lib/image.c

  • Committer: Bazaar Package Importer
  • Author(s): Jack Coulter
  • Date: 2008-04-13 18:48:12 UTC
  • Revision ID: james.westby@ubuntu.com-20080413184812-efc80waq2er6p1bs
Tags: upstream-0.4.2
ImportĀ upstreamĀ versionĀ 0.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//========================================================================
 
2
// GLFW - An OpenGL framework
 
3
// File:        image.c
 
4
// Platform:    Any
 
5
// API version: 2.6
 
6
// WWW:         http://glfw.sourceforge.net
 
7
//------------------------------------------------------------------------
 
8
// Copyright (c) 2002-2006 Camilla Berglund
 
9
//
 
10
// This software is provided 'as-is', without any express or implied
 
11
// warranty. In no event will the authors be held liable for any damages
 
12
// arising from the use of this software.
 
13
//
 
14
// Permission is granted to anyone to use this software for any purpose,
 
15
// including commercial applications, and to alter it and redistribute it
 
16
// freely, subject to the following restrictions:
 
17
//
 
18
// 1. The origin of this software must not be misrepresented; you must not
 
19
//    claim that you wrote the original software. If you use this software
 
20
//    in a product, an acknowledgment in the product documentation would
 
21
//    be appreciated but is not required.
 
22
//
 
23
// 2. Altered source versions must be plainly marked as such, and must not
 
24
//    be misrepresented as being the original software.
 
25
//
 
26
// 3. This notice may not be removed or altered from any source
 
27
//    distribution.
 
28
//
 
29
//========================================================================
 
30
 
 
31
//========================================================================
 
32
// Description:
 
33
//
 
34
// This module acts as an interface for different image file formats (the
 
35
// image file format is detected automatically).
 
36
//
 
37
// By default the loaded image is rescaled (using bilinear interpolation)
 
38
// to the next higher 2^N x 2^M resolution, unless it has a valid
 
39
// 2^N x 2^M resolution. The interpolation is quite slow, even if the
 
40
// routine has been optimized for speed (a 200x200 RGB image is scaled to
 
41
// 256x256 in ~30 ms on a P3-500).
 
42
//
 
43
// Paletted images are converted to RGB/RGBA images.
 
44
//
 
45
// A convenience function is also included (glfwLoadTexture2D), which
 
46
// loads a texture image from a file directly to OpenGL texture memory,
 
47
// with an option to generate all mipmap levels. GL_SGIS_generate_mipmap
 
48
// is used whenever available, which should give an optimal mipmap
 
49
// generation speed (possibly performed in hardware). A software fallback
 
50
// method is included when GL_SGIS_generate_mipmap is not supported (it
 
51
// generates all mipmaps of a 256x256 RGB texture in ~3 ms on a P3-500).
 
52
//
 
53
//========================================================================
 
54
 
 
55
 
 
56
#include "internal.h"
 
57
 
 
58
 
 
59
// We want to support automatic mipmap generation
 
60
#ifndef GL_SGIS_generate_mipmap
 
61
 #define GL_GENERATE_MIPMAP_SGIS       0x8191
 
62
 #define GL_GENERATE_MIPMAP_HINT_SGIS  0x8192
 
63
 #define GL_SGIS_generate_mipmap    1
 
64
#endif // GL_SGIS_generate_mipmap
 
65
 
 
66
 
 
67
//************************************************************************
 
68
//****                  GLFW internal functions                       ****
 
69
//************************************************************************
 
70
 
 
71
//========================================================================
 
72
// _glfwUpsampleImage() - Upsample image, from size w1 x h1 to w2 x h2
 
73
//========================================================================
 
74
 
 
75
static void _glfwUpsampleImage( unsigned char *src, unsigned char *dst,
 
76
    int w1, int h1, int w2, int h2, int bpp )
 
77
{
 
78
    int m, n, k, x, y, col8;
 
79
    float dx, dy, xstep, ystep, col, col1, col2;
 
80
    unsigned char *src1, *src2, *src3, *src4;
 
81
 
 
82
    // Calculate scaling factor
 
83
    xstep = (float)(w1-1) / (float)(w2-1);
 
84
    ystep = (float)(h1-1) / (float)(h2-1);
 
85
 
 
86
    // Copy source data to destination data with bilinear interpolation
 
87
    // Note: The rather strange look of this routine is a direct result of
 
88
    // my attempts at optimizing it. Improvements are welcome!
 
89
    dy = 0.0f;
 
90
    y = 0;
 
91
    for( n = 0; n < h2; n ++ )
 
92
    {
 
93
        dx = 0.0f;
 
94
        src1 = &src[ y*w1*bpp ];
 
95
        src3 = y < (h1-1) ? src1 + w1*bpp : src1;
 
96
        src2 = src1 + bpp;
 
97
        src4 = src3 + bpp;
 
98
        x = 0;
 
99
        for( m = 0; m < w2; m ++ )
 
100
        {
 
101
            for( k = 0; k < bpp; k ++ )
 
102
            {
 
103
                col1 = *src1 ++;
 
104
                col2 = *src2 ++;
 
105
                col = col1 + (col2 - col1) * dx;
 
106
                col1 = *src3 ++;
 
107
                col2 = *src4 ++;
 
108
                col2 = col1 + (col2 - col1) * dx;
 
109
                col += (col2 - col) * dy;
 
110
                col8 = (int) (col + 0.5);
 
111
                if( col8 >= 256 ) col8 = 255;
 
112
                *dst++ = (unsigned char) col8;
 
113
            }
 
114
            dx += xstep;
 
115
            if( dx >= 1.0f )
 
116
            {
 
117
                x ++;
 
118
                dx -= 1.0f;
 
119
                if( x >= (w1-1) )
 
120
                {
 
121
                    src2 = src1;
 
122
                    src4 = src3;
 
123
                }
 
124
            }
 
125
            else
 
126
            {
 
127
                src1 -= bpp;
 
128
                src2 -= bpp;
 
129
                src3 -= bpp;
 
130
                src4 -= bpp;
 
131
            }
 
132
        }
 
133
        dy += ystep;
 
134
        if( dy >= 1.0f )
 
135
        {
 
136
            y ++;
 
137
            dy -= 1.0f;
 
138
        }
 
139
    }
 
140
}
 
141
 
 
142
 
 
143
//========================================================================
 
144
// _glfwHalveImage() - Build the next mip-map level
 
145
//========================================================================
 
146
 
 
147
static int _glfwHalveImage( GLubyte *src, int *width, int *height,
 
148
    int components )
 
149
{
 
150
    int     halfwidth, halfheight, m, n, k, idx1, idx2;
 
151
    GLubyte *dst;
 
152
 
 
153
    // Last level?
 
154
    if( *width <= 1 && *height <= 1 )
 
155
    {
 
156
        return GL_FALSE;
 
157
    }
 
158
 
 
159
    // Calculate new width and height (handle 1D case)
 
160
    halfwidth  = *width > 1 ? *width / 2 : 1;
 
161
    halfheight = *height > 1 ? *height / 2 : 1;
 
162
 
 
163
    // Downsample image with a simple box-filter
 
164
    dst = src;
 
165
    if( *width == 1 || *height == 1 )
 
166
    {
 
167
        // 1D case
 
168
        for( m = 0; m < halfwidth+halfheight-1; m ++ )
 
169
        {
 
170
            for( k = 0; k < components; k ++ )
 
171
            {
 
172
                *dst ++ = (GLubyte) (((int)*src +
 
173
                                      (int)src[components] + 1) >> 1);
 
174
                src ++;
 
175
            }
 
176
            src += components;
 
177
        }
 
178
    }
 
179
    else
 
180
    {
 
181
        // 2D case
 
182
        idx1 = *width*components;
 
183
        idx2 = (*width+1)*components;
 
184
        for( m = 0; m < halfheight; m ++ )
 
185
        {
 
186
            for( n = 0; n < halfwidth; n ++ )
 
187
            {
 
188
                for( k = 0; k < components; k ++ )
 
189
                {
 
190
                    *dst ++ = (GLubyte) (((int)*src +
 
191
                                          (int)src[components] +
 
192
                                          (int)src[idx1] +
 
193
                                          (int)src[idx2] + 2) >> 2);
 
194
                    src ++;
 
195
                }
 
196
                src += components;
 
197
            }
 
198
            src += components * (*width);
 
199
        }
 
200
    }
 
201
 
 
202
    // Return new width and height
 
203
    *width = halfwidth;
 
204
    *height = halfheight;
 
205
 
 
206
    return GL_TRUE;
 
207
}
 
208
 
 
209
 
 
210
//========================================================================
 
211
// _glfwRescaleImage() - Rescales an image into power-of-two dimensions
 
212
//========================================================================
 
213
 
 
214
static int _glfwRescaleImage( GLFWimage* image )
 
215
{
 
216
    int     width, height, log2, newsize;
 
217
    unsigned char *data;
 
218
 
 
219
    // Calculate next larger 2^N width
 
220
    for( log2 = 0, width = image->Width; width > 1; width >>= 1, log2 ++ )
 
221
      ;
 
222
    width  = (int) 1 << log2;
 
223
    if( width < image->Width )
 
224
    {
 
225
        width <<= 1;
 
226
    }
 
227
 
 
228
    // Calculate next larger 2^M height
 
229
    for( log2 = 0, height = image->Height; height > 1; height >>= 1, log2 ++ )
 
230
      ;
 
231
    height = (int) 1 << log2;
 
232
    if( height < image->Height )
 
233
    {
 
234
        height <<= 1;
 
235
    }
 
236
 
 
237
    // Do we really need to rescale?
 
238
    if( width != image->Width || height != image->Height )
 
239
    {
 
240
        // Allocate memory for new (upsampled) image data
 
241
        newsize = width * height * image->BytesPerPixel;
 
242
        data = (unsigned char *) malloc( newsize );
 
243
        if( data == NULL )
 
244
        {
 
245
            free( image->Data );
 
246
            return GL_FALSE;
 
247
        }
 
248
 
 
249
        // Copy old image data to new image data with interpolation
 
250
        _glfwUpsampleImage( image->Data, data, image->Width, image->Height,
 
251
                            width, height, image->BytesPerPixel );
 
252
 
 
253
        // Free memory for old image data (not needed anymore)
 
254
        free( image->Data );
 
255
 
 
256
        // Set pointer to new image data, and set new image dimensions
 
257
        image->Data   = data;
 
258
        image->Width  = width;
 
259
        image->Height = height;
 
260
    }
 
261
 
 
262
    return GL_TRUE;
 
263
}
 
264
 
 
265
 
 
266
//************************************************************************
 
267
//****                    GLFW user functions                         ****
 
268
//************************************************************************
 
269
 
 
270
//========================================================================
 
271
// glfwReadImage() - Read an image from a named file
 
272
//========================================================================
 
273
 
 
274
GLFWAPI int GLFWAPIENTRY glfwReadImage( const char *name, GLFWimage *img,
 
275
    int flags )
 
276
{
 
277
    _GLFWstream stream;
 
278
 
 
279
    // Is GLFW initialized?
 
280
    if( !_glfwInitialized )
 
281
    {
 
282
        return GL_FALSE;
 
283
    }
 
284
 
 
285
    // Start with an empty image descriptor
 
286
    img->Width         = 0;
 
287
    img->Height        = 0;
 
288
    img->BytesPerPixel = 0;
 
289
    img->Data          = NULL;
 
290
 
 
291
    // Open file
 
292
    if( !_glfwOpenFileStream( &stream, name, "rb" ) )
 
293
    {
 
294
        return GL_FALSE;
 
295
    }
 
296
 
 
297
    // We only support TGA files at the moment
 
298
    if( !_glfwReadTGA( &stream, img, flags ) )
 
299
    {
 
300
        _glfwCloseStream( &stream );
 
301
        return GL_FALSE;
 
302
    }
 
303
 
 
304
    // Close stream
 
305
    _glfwCloseStream( &stream );
 
306
 
 
307
    // Should we rescale the image to closest 2^N x 2^M resolution?
 
308
    if( !(flags & GLFW_NO_RESCALE_BIT) )
 
309
    {
 
310
        if( !_glfwRescaleImage( img ) )
 
311
        {
 
312
            return GL_FALSE;
 
313
        }
 
314
    }
 
315
 
 
316
    // Interpret BytesPerPixel as an OpenGL format
 
317
    switch( img->BytesPerPixel )
 
318
    {
 
319
        default:
 
320
        case 1:
 
321
            if( flags & GLFW_ALPHA_MAP_BIT )
 
322
            {
 
323
                img->Format = GL_ALPHA;
 
324
            }
 
325
            else
 
326
            {
 
327
                img->Format = GL_LUMINANCE;
 
328
            }
 
329
            break;
 
330
        case 3:
 
331
            img->Format = GL_RGB;
 
332
            break;
 
333
        case 4:
 
334
            img->Format = GL_RGBA;
 
335
            break;
 
336
    }
 
337
 
 
338
    return GL_TRUE;
 
339
}
 
340
 
 
341
 
 
342
//========================================================================
 
343
// glfwReadMemoryImage() - Read an image file from a memory buffer
 
344
//========================================================================
 
345
 
 
346
GLFWAPI int  GLFWAPIENTRY glfwReadMemoryImage( const void *data, long size, GLFWimage *img, int flags )
 
347
{
 
348
    _GLFWstream stream;
 
349
 
 
350
    // Is GLFW initialized?
 
351
    if( !_glfwInitialized )
 
352
    {
 
353
        return GL_FALSE;
 
354
    }
 
355
 
 
356
    // Start with an empty image descriptor
 
357
    img->Width         = 0;
 
358
    img->Height        = 0;
 
359
    img->BytesPerPixel = 0;
 
360
    img->Data          = NULL;
 
361
 
 
362
    // Open buffer
 
363
    if( !_glfwOpenBufferStream( &stream, (void*) data, size ) )
 
364
    {
 
365
        return GL_FALSE;
 
366
    }
 
367
 
 
368
    // We only support TGA files at the moment
 
369
    if( !_glfwReadTGA( &stream, img, flags ) )
 
370
    {
 
371
        _glfwCloseStream( &stream );
 
372
        return GL_FALSE;
 
373
    }
 
374
 
 
375
    // Close stream
 
376
    _glfwCloseStream( &stream );
 
377
 
 
378
    // Should we rescale the image to closest 2^N x 2^M resolution?
 
379
    if( !(flags & GLFW_NO_RESCALE_BIT) )
 
380
    {
 
381
        if( !_glfwRescaleImage( img ) )
 
382
        {
 
383
            return GL_FALSE;
 
384
        }
 
385
    }
 
386
 
 
387
    // Interpret BytesPerPixel as an OpenGL format
 
388
    switch( img->BytesPerPixel )
 
389
    {
 
390
        default:
 
391
        case 1:
 
392
            if( flags & GLFW_ALPHA_MAP_BIT )
 
393
            {
 
394
                img->Format = GL_ALPHA;
 
395
            }
 
396
            else
 
397
            {
 
398
                img->Format = GL_LUMINANCE;
 
399
            }
 
400
            break;
 
401
        case 3:
 
402
            img->Format = GL_RGB;
 
403
            break;
 
404
        case 4:
 
405
            img->Format = GL_RGBA;
 
406
            break;
 
407
    }
 
408
 
 
409
    return GL_TRUE;
 
410
}
 
411
 
 
412
 
 
413
//========================================================================
 
414
// glfwFreeImage() - Free allocated memory for an image
 
415
//========================================================================
 
416
 
 
417
GLFWAPI void GLFWAPIENTRY glfwFreeImage( GLFWimage *img )
 
418
{
 
419
    // Is GLFW initialized?
 
420
    if( !_glfwInitialized )
 
421
    {
 
422
        return;
 
423
    }
 
424
 
 
425
    // Free memory
 
426
    if( img->Data != NULL )
 
427
    {
 
428
        free( img->Data );
 
429
        img->Data = NULL;
 
430
    }
 
431
 
 
432
    // Clear all fields
 
433
    img->Width         = 0;
 
434
    img->Height        = 0;
 
435
    img->Format        = 0;
 
436
    img->BytesPerPixel = 0;
 
437
}
 
438
 
 
439
 
 
440
//========================================================================
 
441
// glfwLoadTexture2D() - Read an image from a file, and upload it to
 
442
// texture memory
 
443
//========================================================================
 
444
 
 
445
GLFWAPI int GLFWAPIENTRY glfwLoadTexture2D( const char *name, int flags )
 
446
{
 
447
    GLFWimage img;
 
448
 
 
449
    // Is GLFW initialized?
 
450
    if( !_glfwInitialized || !_glfwWin.Opened )
 
451
    {
 
452
        return GL_FALSE;
 
453
    }
 
454
 
 
455
    // Force rescaling if necessary
 
456
    if( !_glfwWin.Has_GL_ARB_texture_non_power_of_two )
 
457
    {
 
458
      flags &= (~GLFW_NO_RESCALE_BIT);
 
459
    }
 
460
 
 
461
    // Read image from file
 
462
    if( !glfwReadImage( name, &img, flags ) )
 
463
    {
 
464
        return GL_FALSE;
 
465
    }
 
466
 
 
467
    if( !glfwLoadTextureImage2D( &img, flags ) )
 
468
    {
 
469
        return GL_FALSE;
 
470
    }
 
471
 
 
472
    // Data buffer is not needed anymore
 
473
    glfwFreeImage( &img );
 
474
 
 
475
    return GL_TRUE;
 
476
}
 
477
 
 
478
 
 
479
//========================================================================
 
480
// glfwLoadMemoryTexture2D() - Read an image from a buffer, and upload it to
 
481
// texture memory
 
482
//========================================================================
 
483
 
 
484
GLFWAPI int  GLFWAPIENTRY glfwLoadMemoryTexture2D( const void *data, long size, int flags )
 
485
{
 
486
    GLFWimage img;
 
487
 
 
488
    // Is GLFW initialized?
 
489
    if( !_glfwInitialized || !_glfwWin.Opened )
 
490
    {
 
491
        return GL_FALSE;
 
492
    }
 
493
 
 
494
    // Force rescaling if necessary
 
495
    if( !_glfwWin.Has_GL_ARB_texture_non_power_of_two )
 
496
    {
 
497
      flags &= (~GLFW_NO_RESCALE_BIT);
 
498
    }
 
499
 
 
500
    // Read image from file
 
501
    if( !glfwReadMemoryImage( data, size, &img, flags ) )
 
502
    {
 
503
        return GL_FALSE;
 
504
    }
 
505
 
 
506
    if( !glfwLoadTextureImage2D( &img, flags ) )
 
507
    {
 
508
        return GL_FALSE;
 
509
    }
 
510
 
 
511
    // Data buffer is not needed anymore
 
512
    glfwFreeImage( &img );
 
513
 
 
514
    return GL_TRUE;
 
515
}
 
516
 
 
517
 
 
518
//========================================================================
 
519
// glfwLoadTextureImage2D() - Upload an image object to texture memory
 
520
//========================================================================
 
521
 
 
522
GLFWAPI int  GLFWAPIENTRY glfwLoadTextureImage2D( GLFWimage *img, int flags )
 
523
{
 
524
    GLint   UnpackAlignment, GenMipMap;
 
525
    int     level, format, AutoGen, newsize, n;
 
526
    unsigned char *data, *dataptr;
 
527
 
 
528
    // Is GLFW initialized?
 
529
    if( !_glfwInitialized || !_glfwWin.Opened )
 
530
    {
 
531
        return GL_FALSE;
 
532
    }
 
533
 
 
534
    // TODO: Use GL_MAX_TEXTURE_SIZE or GL_PROXY_TEXTURE_2D to determine
 
535
    //       whether the image size is valid.
 
536
    // NOTE: May require box filter downsampling routine.
 
537
 
 
538
    // Do we need to convert the alpha map to RGBA format (OpenGL 1.0)?
 
539
    if( (_glfwWin.GLVerMajor == 1) && (_glfwWin.GLVerMinor == 0) &&
 
540
        (img->Format == GL_ALPHA) )
 
541
    {
 
542
        // We go to RGBA representation instead
 
543
        img->BytesPerPixel = 4;
 
544
 
 
545
        // Allocate memory for new RGBA image data
 
546
        newsize = img->Width * img->Height * img->BytesPerPixel;
 
547
        data = (unsigned char *) malloc( newsize );
 
548
        if( data == NULL )
 
549
        {
 
550
            free( img->Data );
 
551
            return GL_FALSE;
 
552
        }
 
553
 
 
554
        // Convert Alpha map to RGBA
 
555
        dataptr = data;
 
556
        for( n = 0; n < (img->Width*img->Height); ++ n )
 
557
        {
 
558
            *dataptr ++ = 255;
 
559
            *dataptr ++ = 255;
 
560
            *dataptr ++ = 255;
 
561
            *dataptr ++ = img->Data[n];
 
562
        }
 
563
 
 
564
        // Free memory for old image data (not needed anymore)
 
565
        free( img->Data );
 
566
 
 
567
        // Set pointer to new image data
 
568
        img->Data = data;
 
569
    }
 
570
 
 
571
    // Set unpack alignment to one byte
 
572
    glGetIntegerv( GL_UNPACK_ALIGNMENT, &UnpackAlignment );
 
573
    glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
 
574
 
 
575
    // Should we use automatic mipmap generation?
 
576
    AutoGen = ( flags & GLFW_BUILD_MIPMAPS_BIT ) &&
 
577
              _glfwWin.Has_GL_SGIS_generate_mipmap;
 
578
 
 
579
    // Enable automatic mipmap generation
 
580
    if( AutoGen )
 
581
    {
 
582
        glGetTexParameteriv( GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,
 
583
            &GenMipMap );
 
584
        glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,
 
585
            GL_TRUE );
 
586
    }
 
587
 
 
588
    // Format specification is different for OpenGL 1.0
 
589
    if( _glfwWin.GLVerMajor == 1 && _glfwWin.GLVerMinor == 0 )
 
590
    {
 
591
        format = img->BytesPerPixel;
 
592
    }
 
593
    else
 
594
    {
 
595
        format = img->Format;
 
596
    }
 
597
 
 
598
    // Upload to texture memeory
 
599
    level = 0;
 
600
    do
 
601
    {
 
602
        // Upload this mipmap level
 
603
        glTexImage2D( GL_TEXTURE_2D, level, format,
 
604
            img->Width, img->Height, 0, format,
 
605
            GL_UNSIGNED_BYTE, (void*) img->Data );
 
606
 
 
607
        // Build next mipmap level manually, if required
 
608
        if( ( flags & GLFW_BUILD_MIPMAPS_BIT ) && !AutoGen )
 
609
        {
 
610
            level = _glfwHalveImage( img->Data, &img->Width,
 
611
                        &img->Height, img->BytesPerPixel ) ?
 
612
                    level + 1 : 0;
 
613
        }
 
614
    }
 
615
    while( level != 0 );
 
616
 
 
617
    // Restore old automatic mipmap generation state
 
618
    if( AutoGen )
 
619
    {
 
620
        glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,
 
621
            GenMipMap );
 
622
    }
 
623
 
 
624
    // Restore old unpack alignment
 
625
    glPixelStorei( GL_UNPACK_ALIGNMENT, UnpackAlignment );
 
626
 
 
627
    return GL_TRUE;
 
628
}
 
629