aboutsummaryrefslogtreecommitdiff
path: root/modules/mod_effects/mod_effects.c
blob: d5a181c46046029256f082f2d58277c37684d3e8 (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
/*
 *  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 <string.h>

#include <math.h>
#include <stdlib.h>

#include "bgddl.h"

#include "libgrbase.h"

#include "mod_effects.h"

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : _get_pixel
 *
 *  Read a pixel from a bitmap
 *
 *  PARAMS :
 *      dest            Destination bitmap
 *      x, y            Pixel coordinates
 *
 *  RETURN VALUE :
 *      1, 8 or 16-bit integer with the pixel value
 *
 */

static int _get_pixel( GRAPH * dest, int x, int y )
{
    if ( x < 0 || y < 0 || x >= ( int )dest->width || y >= ( int )dest->height ) return -1 ;

    switch ( dest->format->depth )
    {
        case 8:
            return *((( uint8_t * )dest->data ) + x + dest->pitch * y) ;

        case 16:
            return *( uint16_t * )((( uint8_t *)dest->data) + ( x << 1 ) + dest->pitch * y) ;

        case 32:
            return *( uint32_t * )((( uint8_t *)dest->data) + ( x << 2 ) + dest->pitch * y) ;

        case 1:
            return ((( uint8_t * )dest->data )[x / 8 + dest->pitch * y] & ( 0x80 >> ( x & 7 ) ) ) ? 1 : 0;
    }
    return -1;
}

/*
 *  FUNCTION : _put_pixel
 *
 *  Paint a pixel with no clipping whatsoever, except by
 *  the bitmap's dimensions
 *
 *  PARAMS :
 *      dest            Destination bitmap
 *      x, y            Pixel coordinates
 *      color           1, 8 or 16-bit pixel value
 *
 *  RETURN VALUE :
 *      None
 *
 */

static void _put_pixel( GRAPH * dest, int x, int y, int color )
{
    if ( x < 0 || y < 0 || x >= ( int )dest->width || y >= ( int )dest->height ) return ;

    dest->modified = 2 ;

    switch ( dest->format->depth )
    {
        case 8:
            *((( uint8_t * )dest->data ) + x + dest->pitch * y) = color ;
            break;

        case 16:
            *( uint16_t * )((( uint8_t *)dest->data) + ( x << 1 ) + dest->pitch * y) = color ;
            break;

        case 32:
            *( uint32_t * )((( uint8_t *)dest->data) + ( x << 2 ) + dest->pitch * y) = color ;
            break;

        case 1:
            if ( color )
                ((( uint8_t * )dest->data )[x / 8 + dest->pitch * y] ) |= ( 0x80 >> ( x & 7 ) ) ;
            else
                ((( uint8_t * )dest->data )[x / 8 + dest->pitch * y] ) &= ~( 0x80 >> ( x & 7 ) ) ;
            break;
    }
}

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

static int modeffects_filter( INSTANCE *my, int *params )
{ //fpg,map,tabla10
    GRAPH * map = bitmap_get( params[0], params[1] ), * map2;
    int *tabla = ( int* )params[2];
    int x, y, i, j;
    int r, g, b, a, r2, g2, b2, c;
    float r1, g1, b1;
    int color;

    if ( !map ) return 0;
    if ( map->format->depth < 16 ) return 0;

    map2 = bitmap_clone( map );

    r1 = 0;
    g1 = 0;
    b1 = 0;
    c = 0;
    for ( i = 0;i < map->width;i++ )
    {
        for ( j = 0;j < map->height;j++ )
        {
            color = _get_pixel( map, i, j );
            if ( !color ) continue;

            gr_get_rgba_depth( map->format->depth, color, &r, &g, &b, &a ); // only need alpha

            for ( y = j - 1;y < j + 2;y++ )
            {
                r2 = 0;
                g2 = 0;
                b2 = 0;
                for ( x = i - 1;x < i + 2;x++, c++ )
                {
                    color = _get_pixel( map, ( x < 0 ) ? 0 : ( x > map->width - 1 ) ? map->width - 1 : x, ( y < 0 ) ? 0 : ( y > map->height - 1 ) ? map->height - 1 : y );
                    if ( !color )
                    {
                        /* Si es transparente, repetimos el ultimo color */
                        r1 += ( float )( r2 * tabla[c] );
                        g1 += ( float )( g2 * tabla[c] );
                        b1 += ( float )( b2 * tabla[c] );
                        continue;
                    }

                    gr_get_rgb_depth( map->format->depth, color, &r2, &g2, &b2 );

                    r1 += ( float )( r2 * tabla[c] );
                    g1 += ( float )( g2 * tabla[c] );
                    b1 += ( float )( b2 * tabla[c] );
                }
            }

            r1 /= tabla[9];
            g1 /= tabla[9];
            b1 /= tabla[9];

            r = ((( int )r1 ) > 255 ) ? 255 : ( int )r1;
            g = ((( int )g1 ) > 255 ) ? 255 : ( int )g1;
            b = ((( int )b1 ) > 255 ) ? 255 : ( int )b1;

            if ( r < 0 )r = 0;
            if ( g < 0 )g = 0;
            if ( b < 0 )b = 0;

            if ( !r && !g && !b )
                c = 0;
            else
                c = gr_rgba_depth( map->format->depth, r, g, b, a );

            _put_pixel( map2, i, j, c );

            r1 = 0;
            g1 = 0;
            b1 = 0;
            c = 0;
        }
    }
    memcpy( map->data, map2->data, map->height*map->pitch );
    bitmap_destroy( map2 );

    return 1 ;
}

static int modeffects_blur( INSTANCE *my, int *params )
{ // fpg,map,tipo
    GRAPH * map = bitmap_get( params[0], params[1] ), *map2;

    int x, y, i, j, c;
    int r, g, b, a, r2, g2, b2, a2;
    int color;

    if ( !map ) return 0;
    if ( map->format->depth < 16 ) return 0;

    switch ( params[2] )
    {
        case BLUR_NORMAL:
            //METODO 1 "RAPIDO" izq y arriba
            for ( i = 0; i < map->width; i++ )
                for ( j = 0; j < map->height; j++ )
                {
                    color = _get_pixel( map, i, j ) ;
                    if ( !color ) continue;
                    gr_get_rgba_depth( map->format->depth, color, &r, &g, &b, &a );
                    if ( i > 0 )
                    {
                        gr_get_rgba_depth( map->format->depth, _get_pixel( map, i - 1, j ), &r2, &g2, &b2, &a2 );
                    }
                    else
                    {
                        gr_get_rgba_depth( map->format->depth, _get_pixel( map, i + 1, j ), &r2, &g2, &b2, &a2 );
                    }
                    r += r2;
                    g += g2;
                    b += b2;
                    if ( j > 0 )
                    {
                        gr_get_rgba_depth( map->format->depth, _get_pixel( map, i, j - 1 ), &r2, &g2, &b2, &a2 );
                    }
                    else
                    {
                        gr_get_rgba_depth( map->format->depth, _get_pixel( map, i, j + 1 ), &r2, &g2, &b2, &a2 );
                    }
                    r += r2;
                    g += g2;
                    b += b2;
                    r /= 3;
                    g /= 3;
                    b /= 3;
                    _put_pixel( map, i, j, gr_rgba_depth( map->format->depth, r, g, b, a ) );
                }
            break;

        case BLUR_3x3:
            // METODO2 LENTO 3x3
            r = 0;
            g = 0;
            b = 0;
            c = 0;
            for ( i = 0;i < map->width;i++ )
                for ( j = 0;j < map->height;j++ )
                {
                    color = _get_pixel( map, i, j ) ;
                    if ( !color ) continue;
                    gr_get_rgba_depth( map->format->depth, color, &r, &g, &b, &a );
                    c++;
                    for ( x = i - 1;x < i + 2;x++ )
                    {
                        for ( y = j - 1;y < j + 2;y++ )
                        {
                            if ( x < 0 || x > map->width - 1 || y < 0 || y > map->height - 1 ) continue;
                            gr_get_rgba_depth( map->format->depth, _get_pixel( map, x, y ), &r2, &g2, &b2, &a2 );
                            r += r2;
                            g += g2;
                            b += b2;
                            c++;
                        }
                    }
                    if ( !c ) continue;
                    r /= c;
                    g /= c;
                    b /= c;
                    _put_pixel( map, i, j, gr_rgba_depth( map->format->depth, r, g, b, a ) );
                    r = 0;
                    g = 0;
                    b = 0;
                    c = 0;
                }
            break;

        case BLUR_5x5:
            // METODO3 aun mas LENTO 5x5
            r = 0;
            g = 0;
            b = 0;
            c = 0;
            for ( i = 0;i < map->width;i++ )
            {
                for ( j = 0;j < map->height;j++ )
                {
                    color = _get_pixel( map, i, j ) ;
                    if ( !color ) continue;
                    gr_get_rgba_depth( map->format->depth, color, &r, &g, &b, &a );
                    c++;
                    for ( x = i - 2;x < i + 3;x++ )
                    {
                        for ( y = j - 2;y < j + 3;y++ )
                        {
                            if ( x < 0 || x > map->width - 1 || y < 0 || y > map->height - 1 ) continue;
                            gr_get_rgba_depth( map->format->depth, _get_pixel( map, x, y ), &r2, &g2, &b2, &a2 );
                            r += r2;
                            g += g2;
                            b += b2;
                            c++;
                        }
                    }
                    if ( !c ) continue;
                    r /= c;
                    g /= c;
                    b /= c;
                    _put_pixel( map, i, j, gr_rgba_depth( map->format->depth, r, g, b, a ) );
                    r = 0;
                    g = 0;
                    b = 0;
                    c = 0;
                }
            }
            break;

        case BLUR_5x5_MAP:
            // METODO4 5x5 mapa adicional
            map2 = bitmap_clone( map );
            r = 0;
            g = 0;
            b = 0;
            c = 0;
            for ( i = 0;i < map->width;i++ )
            {
                for ( j = 0;j < map->height;j++ )
                {
                    color = _get_pixel( map, i, j ) ;
                    if ( !color ) continue;
                    gr_get_rgba_depth( map->format->depth, color, &r, &g, &b, &a );
                    c++;
                    for ( x = i - 2;x < i + 3;x++ )
                    {
                        for ( y = j - 2;y < j + 3;y++ )
                        {
                            if ( x < 0 || x > map->width - 1 || y < 0 || y > map->height - 1 ) continue;
                            gr_get_rgba_depth( map->format->depth, _get_pixel( map, x, y ), &r2, &g2, &b2, &a2 );
                            r += r2;
                            g += g2;
                            b += b2;
                            c++;
                        }
                    }
                    if ( !c ) continue;
                    r /= c;
                    g /= c;
                    b /= c;
                    _put_pixel( map2, i, j, gr_rgba_depth( map->format->depth, r, g, b, a ) );
                    r = 0;
                    g = 0;
                    b = 0;
                    c = 0;
                }
            }
            memcpy( map->data, map2->data, map->height*map->pitch );
            bitmap_destroy( map2 );
            break;
        default:
            break;

    }
    return 1 ;
}

static int modeffects_grayscale( INSTANCE *my, int *params )
{ //fpg,map,tipo
    GRAPH * map = bitmap_get( params[0], params[1] ) ;
    uint32_t i, j, c;
    int r, g, b, a;

    if ( !map ) return 0;
    if ( map->format->depth < 16 ) return 0;

    for ( i = 0;i < map->height;i++ )
        for ( j = 0;j < map->width;j++ )
        {
            gr_get_rgba_depth( map->format->depth, _get_pixel( map, j, i ), &r, &g, &b, &a );
            if ( !r && !g && !b )continue;
            c = ( int )( 0.3 * r + 0.59 * g + 0.11 * b );
            switch ( params[2] )
            {
                case GSCALE_RGB: // RGB
                    c = gr_rgba_depth( map->format->depth, c, c, c, a );
                    break;

                case GSCALE_R: // R
                    c = gr_rgba_depth( map->format->depth, c, 0, 0, a );
                    break;

                case GSCALE_G: // G
                    c = gr_rgba_depth( map->format->depth, 0, c, 0, a );
                    break;

                case GSCALE_B: // B
                    c = gr_rgba_depth( map->format->depth, 0, 0, c, a );
                    break;

                case GSCALE_RG: // RG
                    c = gr_rgba_depth( map->format->depth, c, c, 0, a );
                    break;

                case GSCALE_RB: // RB
                    c = gr_rgba_depth( map->format->depth, c, 0, c, a );
                    break;


                case GSCALE_GB: // GB
                    c = gr_rgba_depth( map->format->depth, 0, c, c, a );
                    break;

                case GSCALE_OFF:
                default:
                    c = gr_rgba_depth( map->format->depth, r, g, b, a );
            }
            _put_pixel( map, j, i, c );
        }

    return 1 ;
}

static int modeffects_rgbscale( INSTANCE *my, int *params )
{ //fpg, map, r, g, b
    GRAPH * map = bitmap_get( params[0], params[1] ) ;
    uint32_t i, j, c;
    int r, g, b, a;

    if ( !map ) return 0;
    if ( map->format->depth < 16 ) return 0;

    for ( i = 0;i < map->height;i++ )
        for ( j = 0;j < map->width;j++ )
        {
            gr_get_rgba_depth( map->format->depth, _get_pixel( map, j, i ), &r, &g, &b, &a );
            if ( !r && !g && !b )continue;
            c = ( int )( 0.3 * r + 0.59 * g + 0.11 * b );
            c = gr_rgba_depth( map->format->depth,
                               ( int )( c * *( float * )( &params[2] ) ),
                               ( int )( c * *( float * )( &params[3] ) ),
                               ( int )( c * *( float * )( &params[4] ) ),
                               a );
            _put_pixel( map, j, i, c );
        }

    return 1 ;
}

/* ----------------------------------------------------------------- */
/* exports                                                           */
/* ----------------------------------------------------------------- */

#include "mod_effects_exports.h"

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