aboutsummaryrefslogtreecommitdiff
path: root/modules/libgrbase/g_conversion.c
blob: 1add7ec38d7db3ad016a5394e43bef2dc975ffa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
 *  Copyright � 2006-2016 SplinterGU (Fenix/Bennugd)
 *  Copyright � 2002-2006 Fenix Team (Fenix)
 *  Copyright � 1999-2002 Jos� Luis Cebri�n Pag�e (Fenix)
 *
 *  This file is part of Bennu - Game Development
 *
 *  This software is provided 'as-is', without any express or implied
 *  warranty. In no event will the authors be held liable for any damages
 *  arising from the use of this software.
 *
 *  Permission is granted to anyone to use this software for any purpose,
 *  including commercial applications, and to alter it and redistribute it
 *  freely, subject to the following restrictions:
 *
 *     1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software
 *     in a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *     2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *
 *     3. This notice may not be removed or altered from any source
 *     distribution.
 *
 */

/* --------------------------------------------------------------------------- */

#include <stdlib.h>
#include "libgrbase.h"

#include "bgddl.h"
#include "dlvaracc.h"

/* --------------------------------------------------------------------------- */

/* Conversion tables - used by 16 bits conversions - 256K */
static uint16_t * convert565ToScreen = NULL ;
static uint16_t * convertScreenTo565 = NULL ;
static int conversion_tables_ok = 0 ;

/* Alpha multiplication tables - multiply a color by its alpha.
 * There will be less than 256 levels
 */
static uint16_t * alpha16[ 256 ];
static uint8_t * alpha8[ 256 ];
static int alpha16_tables_ok = 0 ;
static int alpha8_tables_ok = 0 ;

/* --------------------------------------------------------------------------- */
/* used for variable access                                                    */
/* --------------------------------------------------------------------------- */

enum {
    ALPHA_STEPS = 0
};

/* --------------------------------------------------------------------------- */
/* Son las variables que se desea acceder.                                     */
/* El interprete completa esta estructura, si la variable existe.              */
/* (usada en tiempo de ejecucion)                                              */

DLVARFIXUP __bgdexport( libgrbase, globals_fixup )[] =
{
    /* Nombre de variable global, puntero al dato, tama�o del elemento, cantidad de elementos */
    { "alpha_steps" , NULL, -1, -1 },
    { NULL , NULL, -1, -1 }
};

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : init_alpha16_tables
 *
 *  Init the 16 bit alpha tables (create as many tables as the parameter)
 *  Each alpha table has a 128K memory footprint. Having many alpha tables
 *  provides more transparency values.
 *
 *  PARAMS :
 *  count   Number of tables
 *
 *  RETURN VALUE :
 *      None
 *
 */

static void init_alpha16_tables( int count )
{
    int i, color, inc, next = 0, factor;
    uint16_t * table16 = NULL;

    if ( count <= 0 ) count = 1;
    if ( count > 128 ) count = 128;

    if ( alpha16_tables_ok == count ) return ;

    inc = 256 / count;

    /* Destroy existing tables */

    if ( alpha16_tables_ok )
    {
        for ( table16 = NULL, i = 0 ; i < 256 ; i++ )
        {
            if ( alpha16[ i ] != table16 )
            {
                table16 = alpha16[ i ];
                free( table16 );
            }
            alpha16[ i ] = NULL;
        }
    }

    /* Make new ones */

    for ( i = 0 ; i < 256 ; i++ )
    {
        if ( i == next )
        {
            table16 = malloc( 131072 );
            factor = next + inc / 2;
            next += inc;
            if ( factor > 255 ) factor = 256;

            for ( color = 0 ; color < 65536 ; color++ )
            {
                int r = (( color & sys_pixel_format->Rmask ) >> sys_pixel_format->Rshift << sys_pixel_format->Rloss ) ;
                int g = (( color & sys_pixel_format->Gmask ) >> sys_pixel_format->Gshift << sys_pixel_format->Gloss ) ;
                int b = (( color & sys_pixel_format->Bmask ) >> sys_pixel_format->Bshift << sys_pixel_format->Bloss ) ;

                table16[ color ] =
                    (((( r * factor ) >> 8 ) >> sys_pixel_format->Rloss ) << sys_pixel_format->Rshift ) |
                    (((( g * factor ) >> 8 ) >> sys_pixel_format->Gloss ) << sys_pixel_format->Gshift ) |
                    (((( b * factor ) >> 8 ) >> sys_pixel_format->Bloss ) << sys_pixel_format->Bshift ) ;
            }
        }
        alpha16[ i ] = table16;
    }

    alpha16_tables_ok = count;
}

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : init_alpha8_tables
 *
 *  Init the 8 bit alpha tables (create as many tables as the parameter)
 *  Each alpha table has a 64K memory footprint. Having many alpha tables
 *  provides more transparency values. Those tables should be updated
 *  when the palette changes.
 *
 *  PARAMS :
 *  count   Number of tables
 *
 *  RETURN VALUE :
 *      None
 *
 */

static void init_alpha8_tables( int count )
{
    int i, color, color2, inc, next = 0, factor;
    uint8_t * table8 = NULL;
    rgb_component * rgb ;

    if ( count <= 0 ) count = 1;
    if ( count > 128 ) count = 128;

    if ( alpha16_tables_ok == count ) return ;

    inc = 256 / count;

    /* Destroy existing tables */

    if ( alpha8_tables_ok )
    {
        for ( table8 = NULL, i = 0; i < 256; i++ )
        {
            if ( alpha8[ i ] != table8 )
            {
                table8 = alpha8[ i ];
                free( table8 );
            }
            alpha8[ i ] = NULL;
        }
    }

    /* Make new ones */

    if ( !sys_pixel_format->palette )
        rgb = ( rgb_component * ) default_palette;
    else
        rgb = sys_pixel_format->palette->rgb ;

    for ( i = 0; i < 256; i++ )
    {
        if ( i == next )
        {
            table8 = malloc( 65536 );
            factor = next + inc / 2;
            next += inc;
            if ( factor > 255 ) factor = 255;

            for ( color = 0; color < 256; color++ )
            {
                for ( color2 = 0; color2 < 256; color2++ )
                {
                    int r = ( rgb[ color ].r * factor + rgb[ color2 ].r * ( 255 - factor ) ) >> 8 ;
                    int g = ( rgb[ color ].g * factor + rgb[ color2 ].g * ( 255 - factor ) ) >> 8 ;
                    int b = ( rgb[ color ].b * factor + rgb[ color2 ].b * ( 255 - factor ) ) >> 8 ;
                    table8[( color << 8 ) + color2 ] = gr_find_nearest_color( r, g, b );
                }
                table8[ color ] = color;
            }
        }
        alpha8[ i ] = table8;
    }

    alpha8_tables_ok = count;
}

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : init_conversion_tables
 *
 *  Static routine used to initialize the 16 bits conversion
 *  tables (this only needs to be done once)
 *
 *  PARAMS :
 *  None
 *
 *  RETURN VALUE :
 *      None
 *
 */

static void init_conversion_tables()
{
    uint8_t r, g, b ;
    int n ;

    /* Alloc space for the lookup tables */

    convert565ToScreen = ( uint16_t * ) malloc( sizeof( uint16_t ) * 65536 );
    if ( !convert565ToScreen ) return ;

    convertScreenTo565 = ( uint16_t * ) malloc( sizeof( uint16_t ) * 65536 );
    if ( !convertScreenTo565 )  // No memory
    {
        free( convert565ToScreen );
        return ;
    }

    /* Special case if screen already in 565 format */

    if ( sys_pixel_format->Rmask == 0xF800 &&
            sys_pixel_format->Gmask == 0x07E0 &&
            sys_pixel_format->Bmask == 0x001F )
    {
        for ( n = 0; n < 65536; n++ )
        {
            convert565ToScreen[ n ] = n;
            convertScreenTo565[ n ] = n;
        }
        return ;
    }

    /* Create a fast lookup array */

    for ( n = 0; n < 65536; n++ )
    {
        /* Calculate conversion from 565 to screen format */

        r = (( n >> 8 ) & 0xF8 ) >> sys_pixel_format->Rloss ;
        g = (( n >> 3 ) & 0xFC ) >> sys_pixel_format->Gloss ;
        b = (( n << 3 ) & 0xF8 ) >> sys_pixel_format->Bloss ;

        convert565ToScreen[ n ] = ( r << sys_pixel_format->Rshift ) | ( g << sys_pixel_format->Gshift ) | ( b << sys_pixel_format->Bshift ) ;

        /* Calculate conversion from 565 to screen format */

        r = ((( n & sys_pixel_format->Rmask ) >> sys_pixel_format->Rshift ) << sys_pixel_format->Rloss );
        g = ((( n & sys_pixel_format->Gmask ) >> sys_pixel_format->Gshift ) << sys_pixel_format->Gloss );
        b = ((( n & sys_pixel_format->Bmask ) >> sys_pixel_format->Bshift ) << sys_pixel_format->Bloss );

        convertScreenTo565[ n ] = (( r & 0xF8 ) << 8 ) | (( g & 0xFC ) << 3 ) | (( b & 0xF8 ) >> 3 ) ;
    }
    conversion_tables_ok = 1;
}

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : gr_convert16_565ToScreen
 *
 *  Convert a sequence of 16 bits pixels from 5:6:5 format to
 *  the format used by the screen (usually 5:5:5 or 5:6:5)
 *
 *  PARAMS :
 *  ptr    Pointer to the first pixel
 *  len    Number of pixels (not bytes!)
 *
 *  RETURN VALUE :
 *      None
 *
 */

void gr_convert16_565ToScreen( uint16_t * ptr, int len )
{
    if ( !conversion_tables_ok ) init_conversion_tables();

    while ( len-- )
    {
        *ptr = convert565ToScreen[ *ptr ] ;
        ptr++;
    }
}

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : gr_convert16_ScreenTo565
 *
 *  Convert a sequence of 16 bits pixels in screen format
 *  (usually 5:5:5 or 5:6:5) to the 5:6:5 format used to
 *  store 16 bits pixel values to files in disk
 *
 *  PARAMS :
 *  ptr    Pointer to the first pixel
 *  len    Number of pixels (not bytes!)
 *
 *  RETURN VALUE :
 *      None
 *
 */

void gr_convert16_ScreenTo565( uint16_t * ptr, int len )
{
    if ( !conversion_tables_ok ) init_conversion_tables();

    while ( len-- )
    {
        *ptr = convertScreenTo565[ *ptr ] ;
        ptr++;
    }
}

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : gr_fade16
 *
 *  Fade a 16-bit graphic a given ammount. Fading values are given in percent
 *  (0% = black, 100% = original color, 200% = full color value)
 *
 *  PARAMS :
 *  graph   Pointer to the graphic object
 *  r    Percent of Red component
 *  g    Percent of Green component
 *  b    Percent of Blue component
 *
 *  RETURN VALUE :
 *      None
 *
 */

void gr_fade16( GRAPH * graph, int r, int g, int b )
{
    uint32_t Rtable[ 32 ];
    uint32_t Gtable[ 32 ];
    uint32_t Btable[ 32 ];
    uint32_t x, y;
    uint32_t Rmask;
    uint32_t Rshift;
    uint32_t Gmask;
    uint32_t Gshift;
    uint32_t Bmask;
    uint32_t Bshift;

    for ( x = 0 ; x < 32 ; x++ )
    {
        int c = 8 * x + 7;

        if ( r <= 100 )
            Rtable[ x ] = ( c * r / 100 ) >> sys_pixel_format->Rloss << sys_pixel_format->Rshift;
        else
            Rtable[ x ] = ( c + ( 255 - c ) * ( r - 100 ) / 100 ) >> sys_pixel_format->Rloss << sys_pixel_format->Rshift;

        if ( g <= 100 )
            Gtable[ x ] = ( c * g / 100 ) >> sys_pixel_format->Gloss << sys_pixel_format->Gshift;
        else
            Gtable[ x ] = ( c + ( 255 - c ) * ( g - 100 ) / 100 ) >> sys_pixel_format->Gloss << sys_pixel_format->Gshift;

        if ( b <= 100 )
            Btable[ x ] = ( c * b / 100 ) >> sys_pixel_format->Bloss << sys_pixel_format->Bshift;
        else
            Btable[ x ] = ( c + ( 255 - c ) * ( b - 100 ) / 100 ) >> sys_pixel_format->Bloss << sys_pixel_format->Bshift;
    }

    Rmask = sys_pixel_format->Rmask;
    Gmask = sys_pixel_format->Gmask;
    Bmask = sys_pixel_format->Bmask;

    Rshift = sys_pixel_format->Rshift - sys_pixel_format->Rloss + 3;
    Gshift = sys_pixel_format->Gshift - sys_pixel_format->Gloss + 3;
    Bshift = sys_pixel_format->Bshift - sys_pixel_format->Bloss + 3;

    if ( graph->format->depth == 16 )
    {
        char * p = graph->data ;
        uint16_t * ptr ;

        for ( y = 0 ; y < graph->height ; y++ )
        {
            ptr = ( uint16_t * ) p ;

            for ( x = 0 ; x < graph->width ; x++, ptr++ )
            {
                *ptr = ( Rtable[(( *ptr & Rmask ) >> Rshift )] | Gtable[(( *ptr & Gmask ) >> Gshift )] | Btable[(( *ptr & Bmask ) >> Bshift )] );
            }
            p += graph->pitch ;
        }
    }
    else if ( graph->format->depth == 32 )
    {
        char * p = graph->data ;
        uint32_t * ptr ;

        for ( y = 0 ; y < graph->height ; y++ )
        {
            ptr = ( uint32_t * ) p ;
            for ( x = 0 ; x < graph->width ; x++, ptr++ )
            {
                *ptr = ( Rtable[(( *ptr & Rmask ) >> Rshift )] | Gtable[(( *ptr & Gmask ) >> Gshift )] | Btable[(( *ptr & Bmask ) >> Bshift )] );
            }
            p += graph->pitch ;
        }
    }
}

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : gr_alpha16
 *
 *  Get an alpha multiplication table (a table that, given a 16 bit color,
 *  returns the color multiplied by the alpha value)
 *
 *  PARAMS :
 *  alpha   Alpha value for the requested table
 *
 *  RETURN VALUE :
 *      None
 *
 */

uint16_t * gr_alpha16( int alpha )
{
    if ( !alpha16_tables_ok ) init_alpha16_tables( GLODWORD( libgrbase, ALPHA_STEPS ) );
    return alpha16[ alpha ];
}

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : gr_alpha8
 *
 *  Get an alpha translation table (a table that, given two 8 bit color,
 *  returns the composite color given the alpha value)
 *
 *  PARAMS :
 *  alpha   Alpha value for the requested table
 *
 *  RETURN VALUE :
 *      None
 *
 */

uint8_t * gr_alpha8( int alpha )
{
    if ( !alpha8_tables_ok ) init_alpha8_tables( GLODWORD( libgrbase, ALPHA_STEPS ) );
    return ( uint8_t * ) alpha8[ alpha ];
}

/* --------------------------------------------------------------------------- */