File: g_pal.c

package info (click to toggle)
fenix 0.92a.dfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 8,492 kB
  • sloc: ansic: 42,357; sh: 3,474; perl: 527; makefile: 180; cpp: 19
file content (1010 lines) | stat: -rw-r--r-- 27,670 bytes parent folder | download | duplicates (4)
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
/*
 *  Fenix - Videogame compiler/interpreter
 *  Current release       : FENIX - PROJECT 1.0 - R 0.84
 *  Last stable release   :
 *  Project documentation : http://fenix.divsite.net
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 *  Copyright � 1999 Jos� Luis Cebri�n Pag�e
 *  Copyright � 2002 Fenix Team
 *
 */

/*
 * FILE        : g_pal.c
 * DESCRIPTION : Palette and color functions
 *
 * HISTORY:      0.82 - Blendops functions moved to g_blendop.c
 */

#ifdef TARGET_BEOS
#include <posix/assert.h>
#else
#include <assert.h>
#endif

#include <string.h>
#include <stdlib.h>

#ifdef USE_GETTEXT
#include <libintl.h>
#define _(String) gettext (String)
#else
#define _(String) String
#endif

#include "fxi.h"
#include "fgc.h"
#include "fbm.h"
#include "fpl.h"

extern GRAPH * gr_read_png (const char * filename) ;

static unsigned char colors[256][3] ;

PALETTE * first_palette = NULL ;

SDL_Color  palette[256] ;

SDL_Color vpalette[256] ;
Uint16     colorequiv[256] ;    /* Equivalencia paleta -> pantalla   */

Uint16     colorghost[65536] ;  /* Deja un color a 50% de intensidad */

int palette_loaded = 0 ;
int palette_changed = 0 ;

static int fade_inc = 0 ;
int        fade_on = 0 ;
int        fade_set = 0 ;
int        fade_step = 64 ;
SDL_Color  fade_from ;
SDL_Color  fade_to ;
SDL_Color  fade_pos = { 100, 100, 100 };

/* Lista ordenada de colores */

int color_list[256] ;

/* Tabla de transparencias */

Uint8 trans_table[256][256] ;
int trans_table_updated = 0 ;

/* Paleta de colores por defecto */

int default_palette[] =
{
        0,  0,  0,  16, 16, 16,  32, 32, 32,  48, 48, 48,
       64, 64, 64,  84, 84, 84, 100,100,100, 116,116,116,
      132,132,132, 148,148,148, 168,168,168, 184,184,184,
      200,200,200, 216,216,216, 232,232,232, 252,252,252,
       40,  0,  0,  72,  0,  0, 108,  0,  0, 144,  0,  0,
      180,  0,  0, 216,  0,  0, 252,  0,  0, 252, 32,  0,
      252, 64,  0, 252, 96,  0, 252,128,  0, 252,160,  0,
      252,180, 56, 252,200,112, 252,220,168, 252,240,224,
        0, 40,  0,   0, 60,  0,   0, 84,  0,   0,108,  0,
        0,132,  0,   0,156,  0,   0,180,  0,   0,204,  0,
        0,228,  0,   0,252,  0,  48,252, 32,  96,252, 64,
      144,252, 96, 192,252,132, 216,252,176, 240,252,220,
        0,  0, 40,   0,  0, 72,   0,  0,104,   0,  0,140,
        0,  0,172,   0,  0,208,   0,  0,252,   0, 48,252,
        0,100,252,   0,148,252,   0,200,252,   0,252,252,
       56,252,252, 112,252,252, 168,252,252, 224,252,252,
       28, 28,  0,  52, 52,  0,  76, 76,  0, 100,100,  0,
      124,124,  0, 152,152,  0, 176,176,  0, 200,200,  0,
      224,224,  0, 252,252,  0, 252,252, 36, 252,252, 72,
      252,252,108, 252,252,144, 252,252,180, 252,252,220,
       28,  0, 28,  52,  0, 52,  76,  0, 76, 100,  0,100,
      124,  0,124, 152,  0,152, 176,  0,176, 200,  0,200,
      224,  0,224, 252,  0,252, 252, 36,252, 252, 72,252,
      252,112,252, 252,148,252, 252,184,252, 252,224,252,
        0, 20, 20,   0, 40, 40,   0, 60, 60,   0, 80, 80,
        0,104,100,   0,124,120,   0,144,144,   0,164,164,
        0,188,184,   0,208,204,   0,228,224,   0,252,248,
       44,252,248,  92,252,248, 140,252,248, 188,252,248,
       24, 12,  0,  44, 24,  0,  64, 36,  0,  84, 48,  0,
      108, 60,  0, 128, 72,  0, 148, 84,  0, 168, 96,  0,
      192,112,  0, 196,128, 28, 204,144, 60, 212,160, 92,
      216,180,120, 224,196,152, 232,212,184, 240,232,216,
       24, 12, 12,  40, 20, 20,  60, 32, 32,  80, 44, 44,
       96, 52, 52, 116, 64, 64, 136, 76, 76, 156, 88, 88,
      176,104,104, 196,120,120, 216,136,136, 240,152,152,
      240,168,168, 244,188,188, 244,204,204, 248,224,224,
       24, 20, 12,  44, 36, 24,  68, 52, 36,  88, 72, 48,
      112, 88, 60, 132,104, 72, 156,124, 88, 172,140,100,
      188,156,112, 204,176,124, 220,192,136, 240,212,152,
      240,216,168, 244,224,188, 244,232,204, 248,240,224,
       32,  8,  0,  60, 16,  0,  88, 28,  0, 120, 36,  0,
      148, 48,  0, 176, 56,  0, 208, 68,  0, 216, 88,  0,
      224,112,  0, 232,136,  0, 240,160,  0, 252,184,  0,
      252,200, 56, 252,216,112, 252,232,168, 252,252,224,
       20, 12, 12,  36, 24, 24,  56, 36, 36,  72, 48, 48,
       92, 64, 64, 108, 76, 76, 128, 88, 88, 144,100,100,
      164,116,116, 172,132,132, 184,148,148, 192,164,164,
      204,180,180, 212,196,196, 224,212,212, 236,232,232,
       12, 20, 12,  24, 36, 24,  36, 56, 36,  48, 72, 48,
       64, 92, 64,  76,108, 76,  88,128, 88, 100,144,100,
      116,164,116, 132,172,132, 148,184,148, 164,192,164,
      180,204,180, 196,212,196, 212,224,212, 232,236,232,
       12, 12, 16,  24, 24, 32,  36, 36, 48,  48, 48, 64,
       64, 64, 80,  76, 76, 96,  88, 88,112, 100,100,128,
      116,116,148, 132,132,160, 148,148,172, 164,164,184,
      180,180,196, 196,196,208, 212,212,220, 232,232,236,
       40,  0,  0,  80,  0,  0, 124,  0,  0, 164,  0,  0,
      208,  0,  0, 252,  0,  0, 252, 40,  0, 252, 84,  0,
      252,124,  0, 252,168,  0, 252,208,  0, 252,252,  0,
      252,252, 44, 252,252, 92, 252,252,140, 252,252,188,
        0,  0,  0,   0,  0, 88,   0,  0,128,   0,  0,168,
        0,  0,208,   0,  0,248,  40,  0,248,  84,  0,248,
      124,  0,248, 168,  0,248, 208,  0,248, 252,  0,252,
      252, 52,252, 252,108,252, 252,164,252, 252,220,252
} ;

void pal_refresh(PALETTE * pal) {
    PALETTE * p ;
    int n;

    if (enable_16bits) {
        if (!pal)
            p = first_palette;
        else
            p = pal ;

        while (p) {
            for (n = 0 ; n < 256 ; n++)
                p->colorequiv[n] = SDL_MapRGB (screen->format, p->rgb[n].r, p->rgb[n].g, p->rgb[n].b);
            p = p->next;
            if (pal) break;
        }
    }
}


void pal_use(PALETTE * pal)
{
    if (pal) pal->use++;
}

int pal_discard(PALETTE * pal)
{
    if (!pal) return 0;
    if (pal->use) pal->use--;
    return pal->use;
}

PALETTE * pal_new(PALETTE * basepal)
{
    PALETTE * pal = malloc(sizeof(PALETTE));

    if (!pal) return NULL ;

    if (basepal) {
        memmove(pal->rgb, basepal->rgb, sizeof(pal->rgb)) ;
        memmove(pal->colorequiv, basepal->colorequiv, sizeof(pal->colorequiv)) ;
    } else {
        /* Default */
        memmove(pal->rgb, palette, sizeof(pal->rgb)) ;
        if (enable_16bits) {
            int n;
            for (n = 0 ; n < 256 ; n++)
                pal->colorequiv[n] = SDL_MapRGB (screen->format, pal->rgb[n].r, pal->rgb[n].g, pal->rgb[n].b) ;
        }
    }

    pal->use        = 1 ;
    pal->next       = first_palette ;
    pal->prev       = NULL ;
    first_palette   = pal ;

    return pal;
}

PALETTE * pal_new2(unsigned char * datapal)
{
    PALETTE * pal = malloc(sizeof(PALETTE));
    int n;

    if (!pal) return NULL ;

    if (datapal) {
		for (n = 0 ; n < 256 ; n++)
		{
			pal->rgb[n].r = datapal[3*n    ];
			pal->rgb[n].g = datapal[3*n + 1];
			pal->rgb[n].b = datapal[3*n + 2];
		}
    } else {
        /* Default */
        memmove(pal->rgb, palette, sizeof(pal->rgb)) ;
    }

    if (enable_16bits) {
        for (n = 0 ; n < 256 ; n++)
            pal->colorequiv[n] = SDL_MapRGB (screen->format, pal->rgb[n].r, pal->rgb[n].g, pal->rgb[n].b) ;
    }

    pal->use        = 1 ;
    pal->next       = first_palette ;
    pal->prev       = NULL ;
    first_palette   = pal ;

    return pal;
}

void pal_destroy(PALETTE * pal)
{
    if (!pal) return ;

    if (pal_discard(pal)) return ;

    if (pal->next) pal->next->prev = pal->prev ;
    if (pal->prev) pal->prev->next = pal->next ;

    if (pal == first_palette) first_palette = pal->next ;

    free (pal);
}

int pal_get (PALETTE * spal, int color, int num, Uint8 * pal)
{
    if (!spal || num < 1 || color < 0 || color > 255) return 0;
    if (color+num >= 256) num = 256-color ;

    while (num--)
    {
        *pal++ = spal->rgb[color  ].r ;
        *pal++ = spal->rgb[color  ].g ;
        *pal++ = spal->rgb[color++].b ;
    }
    return num;
}

int pal_set (PALETTE * spal, int color, int num, Uint8 * pal)
{
    if (!spal || num < 1 || color < 0 || color > 255) return 0;
    if (color+num >= 256) num = 256-color ;

    while (num--)
    {
        spal->rgb[color  ].r = *pal++ ;
        spal->rgb[color  ].g = *pal++ ;
        spal->rgb[color++].b = *pal++ ;
    }
    return num;
}

int pal_map_assign (int libid, int mapcode, PALETTE * palid)
{
    GRAPH * bmp = bitmap_get (libid, mapcode) ;
    PALETTE * oldpal ;
    if (!bmp || bmp->depth != 8 || !palid) return 0 ;
    oldpal = bmp->palette ;
    bmp->palette = palid ;
    pal_use(bmp->palette) ;
    if (oldpal) pal_destroy(oldpal) ;
    return 1 ;
}

int pal_map_remove (int libid, int mapcode)
{
    GRAPH * bmp = bitmap_get (libid, mapcode) ;
    if (!bmp || bmp->depth != 8) return 0 ;
    if (bmp->palette) pal_destroy (bmp->palette) ;
    bmp->palette = NULL ;
    return 1 ;
}

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

static void activate_vpalette()
{
    int n ;

    if (!screen) return ;

    if (enable_16bits)
    {
        for (n = 0 ; n < 256 ; n++)
            colorequiv[n] = SDL_MapRGB (screen->format, palette[n].r, palette[n].g, palette[n].b) ;
    }
    else
    {
        for (n = 0 ; n < 256 ; n++)
        {
            if (fade_pos.r <= 100)
                vpalette[n].r = palette[n].r * fade_pos.r / 100;
            else
                vpalette[n].r = palette[n].r + (255-palette[n].r) * (fade_pos.r-100) / 100;

            if (fade_pos.g <= 100)
                vpalette[n].g = palette[n].g * fade_pos.g / 100;
            else
                vpalette[n].g = palette[n].g + (255-palette[n].g) * (fade_pos.g-100) / 100;

            if (fade_pos.b <= 100)
                vpalette[n].b = palette[n].b * fade_pos.b / 100;
            else
                vpalette[n].b = palette[n].b + (255-palette[n].b) * (fade_pos.b-100) / 100;
        }

        SDL_SetColors (screen, vpalette, 0, 256) ;
    }
}

void gr_roll_palette (int color0, int num, int inc)
{
    SDL_Color backup[256] ;

    if (!scr_initialized) gr_init (320, 200) ;

    if (color0 < 0 || color0 > 255) return ;
    if (num+color0 > 255) num = 256-color0 ;
    while (inc >= num) inc -= num ;
    while (inc <= -num) inc += num ;
    if (!inc) return ;

        memcpy  (&backup[color0],  &palette[color0],     sizeof(SDL_Color)*num) ;

    if (inc < 0) /* Derecha */
    {
        inc = -inc ;
        memmove (&palette[color0+inc], &palette[color0], sizeof(SDL_Color)*(num-inc)) ;
        memcpy  (&palette[color0], &backup[color0+num-inc], sizeof(SDL_Color)*inc) ;
    }
    else /* Izquierda */
    {
        memmove (&palette[color0], &palette[color0+inc], sizeof(SDL_Color)*(num-inc)) ;
        memcpy  (&palette[color0+num-inc], &backup[color0], sizeof(SDL_Color)*inc) ;
    }

    palette_changed = 1 ;
}

void gr_fade_init (int r, int g, int b, int speed)
{
    if (fade_pos.r == r && fade_pos.g == g && fade_pos.b == b) return;

    fade_inc  = speed;
    fade_step = 0;
    fade_on   = 1 ;
    fade_from = fade_pos;
    fade_to.r = (r > 200) ? 200 : r ;
    fade_to.g = (g > 200) ? 200 : g ;
    fade_to.b = (b > 200) ? 200 : b ;

    GLODWORD(FADING) = 1 ;
}

void gr_fade_step()
{
    if (fade_on)
    {
        fade_set = 1 ;
        GLODWORD(FADING) = 1 ;

        fade_step += fade_inc ;
        if (fade_step < 0)
        {
            GLODWORD(FADING) = 0 ;
            fade_step = 0 ;
            fade_on = 0 ;
        }
        if (fade_step >= 64)
        {
            GLODWORD(FADING) = 0 ;
            fade_step = 64 ;
            fade_on = 0 ;
        }

        fade_pos.r = (fade_to.r * fade_step + fade_from.r * (64-fade_step)) / 64;
        fade_pos.g = (fade_to.g * fade_step + fade_from.g * (64-fade_step)) / 64;
        fade_pos.b = (fade_to.b * fade_step + fade_from.b * (64-fade_step)) / 64;

        if ((fade_step + fade_inc < 0 || fade_step + fade_inc > 64) &&
            (fade_pos.r == 100 && fade_pos.g == 100 && fade_pos.b == 100))
        {
            GLODWORD(FADING) = 0 ;
            fade_step = 100 ;
            fade_on = 0;
        }
    }

    if (fade_set)
    {
        if (!fade_on && fade_to.r == 100 && fade_to.g == 100 && fade_to.b == 100) {
            fade_set = 0;
        }

        activate_vpalette() ;

        if (scrbitmap->depth == 16)
        {
            gr_fade16 (scrbitmap,  fade_pos.r, fade_pos.g, fade_pos.b);
        }
    }

}

#if 0
int gr_read_pal (file * fp)
{
    int i ;

    if (!file_read (fp, colors, 3 * 256)) return 0 ;

    /* Ignora definiciones de gama */
    file_seek (fp, 576, SEEK_CUR) ;

    for (i = 0 ; i < 256 ; i++)
    {
        palette[i].r = colors[i][0] << 2 ;
        palette[i].g = colors[i][1] << 2 ;
        palette[i].b = colors[i][2] << 2 ;
    }

    palette_loaded = 1 ;
    palette_changed = 1 ;

    return 1 ;
    }
#endif

PALETTE * gr_read_pal (file * fp)
{
    int i ;
    PALETTE * pal;

    if (!file_read (fp, colors, 3 * 256)) return NULL ;

    for (i = 0 ; i < 256 ; i++)
    {
        colors[i][0] = colors[i][0] << 2 ;
        colors[i][1] = colors[i][1] << 2 ;
        colors[i][2] = colors[i][2] << 2 ;
        if (!palette_loaded) {
            palette[i].r = colors[i][0] ;
            palette[i].g = colors[i][1] ;
            palette[i].b = colors[i][2] ;
        }
    }

    pal = pal_new2(colors);

    palette_loaded = 1 ;
    palette_changed = 1 ;

    return pal ;
}

PALETTE * gr_read_pal_with_gamma (file * fp)
{
    PALETTE * pal = gr_read_pal (fp);

    /* Ignora definiciones de gama */
    if (pal) file_seek (fp, 576, SEEK_CUR) ;

    return pal ;
}


/*
 *  FUNCTION : gr_save_pal
 *
 *  Saves the current palette to the given file
 *
 *  PARAMS :
 *      filename        Name of file to create
 *
 *  RETURN VALUE :
 *      1 if succeded, 0 if error
 */

int gr_save_pal (const char * filename)
{
    file * fp = file_open (filename, "wb");
    char header[8] = "pal\x1A\x0D\x0A";
    int i;

    if (!fp)
        return 0;

    for (i = 0 ; i < 256 ; i++)
    {
        colors[i][0] = palette[i].r / 4 ;
        colors[i][1] = palette[i].g / 4 ;
        colors[i][2] = palette[i].b / 4 ;
    }

    file_write (fp, header, 8);
    file_write (fp, colors, 768);

    memset (colors, 0, 576);
    file_write (fp, colors, 576);
    file_close (fp);

    return 1;
}

int gr_load_pal (const char * filename)
{
    file * fp = file_open (filename, "rb") ;
    char header[8], headerEx[16] ;
    FGC_HEADER  fgcHeader;
    FBM_FILE_HEADER fbmHeader;
    int r = 0 ;
    int i = 0 ;

    if (!fp) return 0 ;
    file_read (fp, header, 8) ;
    if (strcmp (header, "map\x1A\x0D\x0A") == 0)
    {
        file_seek (fp, 48, SEEK_SET) ;
        r = gr_read_pal_with_gamma (fp) ;
    }
    else if (strcmp (header, "fpg\x1A\x0D\x0A") == 0 ||
        strcmp (header, "fnt\x1A\x0D\x0A") == 0 ||
        strcmp (header, "pal\x1A\x0D\x0A") == 0)
    {
        r = gr_read_pal_with_gamma (fp) ;
    }
    else if (memcmp (header, "\x89PNG", 4) == 0)
    {
        GRAPH * graph ;

        file_close (fp);
        palette_loaded = 0;
        graph = gr_read_png(filename);
        if (graph) {
            r = (int) graph->palette;
            pal_use(graph->palette);
            bitmap_destroy(graph);
        }
    }
    else {
        // Soporte para FGC
        file_seek(fp, 0, SEEK_SET) ;
        file_read (fp, headerEx, 16) ;
        if (strcmp (headerEx, FGC_MAGIC) == 0) {
            file_seek (fp, 0, SEEK_SET) ;
            if (file_read (fp, &fgcHeader, sizeof(FGC_HEADER)) != sizeof(FGC_HEADER))
            {
                fgc_error = _("FGC file corrupt or truncated");
                file_close(fp);
                return 0;
            }
            ARRANGE_DWORD(&fgcHeader.version);
            ARRANGE_DWORD(&fgcHeader.depth);
            ARRANGE_DWORD(&fgcHeader.count);
            ARRANGE_DWORD(&fgcHeader.palette);

            if (fgcHeader.depth == 8) {

                file_seek (fp, fgcHeader.palette, SEEK_SET) ;
                r = gr_read_pal (fp);


/*
                if (file_read(fp, colors, 768) != 768) {
                    fgc_error = _("PALETTE - FGC file is truncated");
                    file_close(fp);
                    return 0;
                }

                for (i = 0 ; i < 256 ; i++) {
                    palette[i].r = colors[i][0];
                    palette[i].g = colors[i][1];
                    palette[i].b = colors[i][2];

                }
                palette_loaded = 1 ;
                palette_changed = 1 ;
                r = 1 ;
*/
            } else {
                fgc_error = _("FGC file does not contain information about the colour palette");
                file_close(fp);
                return 0;
            }
        }
        else if (strcmp (header, FBM_MAGIC) == 0) {
            // Soporte para FBM

            file_seek (fp, 0, SEEK_SET) ;

            if (file_read(fp,&fbmHeader,sizeof(FBM_FILE_HEADER)!=sizeof(FBM_FILE_HEADER))) {
                fbm_error = _("FBM file corrupt or truncated");
                file_close(fp);
                return 0;
            }

            ARRANGE_DWORD(&fbmHeader.depth);

            if (fbmHeader.depth!=8) {
                fbm_error = _("FBM file does not contain information about the colour palette");
                file_close(fp);
                return 0;
            }

            file_seek (fp, sizeof(FBM_FILE_HEADER)+sizeof(FBM_HEADER), SEEK_SET) ;


            r = gr_read_pal (fp);
/*
            if (file_read(fp, colors, 768) != 768) {
                fbm_error = _("PALETTE - FGC file is truncated");
                file_close(fp);
                return 0;
            }

            for (i = 0 ; i < 256 ; i++) {
                palette[i].r = colors[i][0];
                palette[i].g = colors[i][1];
                palette[i].b = colors[i][2];
            }

            palette_loaded = 1 ;
            palette_changed = 1 ;
            r = 1 ;
*/

        } else if (strcmp (header, FPL_MAGIC) == 0) {

            // FPL SUPPORT

            file_seek (fp, 0, SEEK_SET) ;
            r = fpl_load_from(fp) ;

        }

    }
    file_close(fp) ;
    return r;
}

int gr_rgb (int r, int g, int b)
{
    int color ;

    if (!scr_initialized) gr_init (320, 200) ;

    r >>= screen->format->Rloss ;
    g >>= screen->format->Gloss ;
    b >>= screen->format->Bloss ;

    color= (r << screen->format->Rshift) | (g << screen->format->Gshift) | (b << screen->format->Bshift) ;
    if (!color) return 1 ;
    return color ;
}

void gr_get_rgb (int color, int *r, int *g, int *b)
{
    if (!scr_initialized) gr_init (320, 200) ;

    if (!enable_16bits)
    {
        color &= 0xFF ;
        (*r) = palette[color].r ;
        (*g) = palette[color].g ;
        (*b) = palette[color].b ;
        return ;
    }

    (*r) = ((color & screen->format->Rmask) >> screen->format->Rshift) ;
    (*g) = ((color & screen->format->Gmask) >> screen->format->Gshift) ;
    (*b) = ((color & screen->format->Bmask) >> screen->format->Bshift) ;

    (*r) <<= screen->format->Rloss ;
    (*g) <<= screen->format->Gloss ;
    (*b) <<= screen->format->Bloss ;
}

/* Busca el color m�s cercano en la paleta a uno determinado */

/* Emplea una cach� interna que acelera considerablemente el proceso,
 * a cambio de perder una parte de precisi�n */

Uint8 nearest_table[64][64][64] ;

inline int find_nearest_color (const int first, const int last, SDL_Color key)
{
    int n, diff ;
    int best, bestdiff;
    SDL_Color * c ;

    n = nearest_table[key.r/4][key.g/4][key.b/4] ;

    if (!n && key.r/4 > 0) n = nearest_table[key.r/4-1][key.g/4][key.b/4] ;
    if (!n && key.g/4 > 0) n = nearest_table[key.b/4][key.g/4-1][key.b/4] ;
    if (!n && key.b/4 > 0) n = nearest_table[key.b/4][key.g/4][key.b/4-1] ;
    if (n) return n ;

    //printf ("*** %02d %02d %02d NOT FOUND ****\n", key.r/8, key.g/8, key.b/8) ;

    bestdiff = 999 ;
    best     = 0;

    for (n = first ; n <= last ; n++)
    {
        c = &palette[color_list[n]] ;
        diff =  ((key.r > c->r) ? key.r-c->r : c->r-key.r)
             +  ((key.g > c->g) ? key.g-c->g : c->g-key.g)
             +  ((key.b > c->b) ? key.b-c->b : c->b-key.b) ;
        if (diff < bestdiff) {
            bestdiff = diff ;
            best     = n ;
        }
    }

    return nearest_table[key.r/4][key.g/4][key.b/4] = color_list[best] ;
}

int gr_find_nearest_color (int r, int g, int b)
{
    SDL_Color c ;
    c.r = r ;
    c.g = g ;
    c.b = b ;

    return find_nearest_color(0, 255, c) ;
}

Uint16 gr_get_color (int r, int g, int b)
{
    return SDL_MapRGB (screen->format, (Uint8)r, (Uint8)g, (Uint8)b) ;
}

/* ----------------------------------------------------------------------
 * Funci�n que rellena la tabla de color m�s cercano (nearest_table)
 * ---------------------------------------------------------------------- */

#define DUP 1
#define DDOWN 2
#define DRIGHT 3
#define DLEFT 4
#define DIN 5
#define DOUT 6

typedef struct __fillpoint {
        Uint8 r, g, b, dir, color ;
        struct __fillpoint * next ;
        struct __fillpoint * prev ;
} fillpoint ;

static fillpoint * filling = NULL ;

inline fillpoint * add_filling(int r, int g, int b, int color, int dir)
{
    fillpoint * n ;

    if (nearest_table[r*2][g*2][b*2] != 0)
        return NULL ;

    nearest_table[r*2+0][g*2+0][b*2+0] = color ;
    nearest_table[r*2+0][g*2+0][b*2+1] = color ;
    nearest_table[r*2+0][g*2+1][b*2+0] = color ;
    nearest_table[r*2+0][g*2+1][b*2+1] = color ;
    nearest_table[r*2+1][g*2+0][b*2+0] = color ;
    nearest_table[r*2+1][g*2+0][b*2+1] = color ;
    nearest_table[r*2+1][g*2+1][b*2+0] = color ;
    nearest_table[r*2+1][g*2+1][b*2+1] = color ;

    n = malloc(sizeof(fillpoint)) ;
    n->r = r ;
    n->g = g ;
    n->b = b ;
    n->color = color ;
    n->dir = dir ;
    n->next = filling ;
    n->prev = NULL ;

    if (filling)
        filling->prev = n ;

    return filling = n ;
}

inline void del_filling(fillpoint * n)
{
    if (n->next)
        n->next->prev = n->prev ;

    if (n->prev)
        n->prev->next = n->next ;
    else
        filling = n->next ;

    free (n) ;
}

void gr_fill_nearest_table()
{
    int n ;
    SDL_Color c ;
    register fillpoint * ptr = NULL ;
    fillpoint * next = NULL ;

    for (n = 1 ; n < 256 ; n++)
    {
        c = palette[n] ;
        c.r /= 8 ;
        c.g /= 8 ;
        c.b /= 8 ;
        add_filling (c.r, c.g, c.b, n, DRIGHT) ;
        if (c.r < 31) add_filling (c.r+1, c.g,   c.b,   n, DRIGHT) ;
        if (c.r > 0)  add_filling (c.r-1, c.g,   c.b,   n, DLEFT) ;
        if (c.g < 31) add_filling (c.r,   c.g+1, c.b,   n, DDOWN) ;
        if (c.g > 0)  add_filling (c.r,   c.g-1, c.b,   n, DUP) ;
        if (c.b < 31) add_filling (c.r,   c.g,   c.b+1, n, DIN) ;
        if (c.b > 0)  add_filling (c.r,   c.g,   c.b-1, n, DOUT) ;
    }

    while (filling)
    {
        ptr = filling ;
        while (ptr)
        {
            if (ptr->r < 31) add_filling(ptr->r+1, ptr->g,   ptr->b,   ptr->color, DRIGHT) ;
            if (ptr->r > 0)  add_filling(ptr->r-1, ptr->g,   ptr->b,   ptr->color, DLEFT) ;
            if (ptr->g < 31) add_filling(ptr->r,   ptr->g+1, ptr->b,   ptr->color, DUP) ;
            if (ptr->g > 0)  add_filling(ptr->r,   ptr->g-1, ptr->b,   ptr->color, DDOWN) ;
            if (ptr->b < 31) add_filling(ptr->r,   ptr->g,   ptr->b+1, ptr->color, DIN) ;
            if (ptr->b > 0)  add_filling(ptr->r,   ptr->g,   ptr->b-1, ptr->color, DOUT) ;
            next = ptr->next ;
            del_filling (ptr) ;
            ptr = next ;
        }
    }

}

/* ----------------------------------------------------------------------
 * Rellena el contenido de la tabla de transparencias
 * ---------------------------------------------------------------------- */

static int compare_colors (const void * p1, const void * p2)
{
    SDL_Color * c1 = &palette[*(int *)p1] ;
    SDL_Color * c2 = &palette[*(int *)p2] ;

    return (c1->r + c1->g + c1->b) - (c2->r + c2->g + c2->b) ;
}

void gr_make_trans_table()
{
    int ticks1, ticks2 ;
    int a=0, b=0, c1=0, c2=0, n=0 ;
    SDL_Color base, dest ;

    if (trans_table_updated) return;

    if (report_graphics) ticks1 = SDL_GetTicks() ;

    memset (nearest_table, 0, sizeof(nearest_table)) ;
    for (n = 1 ; n < 256 ; n++)
    {
        SDL_Color *c = &palette[n] ;
        nearest_table[c->r/4][c->g/4][c->b/4] = n ;
    }

    for (n = 0 ; n < 256 ; n++) color_list[n] = n ;
    qsort (color_list, 256, sizeof(int), compare_colors) ;

    gr_fill_nearest_table() ;

    for (a = 0 ; a < 256 ; a++)
    {
        c1 = color_list[a] ;
        base.r = palette[c1].r/2 ;
        base.g = palette[c1].g/2 ;
        base.b = palette[c1].b/2 ;

        for (b = 0 ; b < a ; b++)
        {
            c2 = color_list[b] ;
            dest.r = base.r + palette[c2].r/2 ;
            dest.g = base.g + palette[c2].g/2 ;
            dest.b = base.b + palette[c2].b/2 ;

            trans_table[c1][c2] = trans_table[c2][c1] = find_nearest_color (b, a, dest) ;
        }

        trans_table[c1][c1] = c1 ;
        trans_table[0][c1]  = c1 ;
    }

    trans_table_updated = 1 ;

    if (report_graphics) {
        ticks2 = SDL_GetTicks() ;
        gr_con_printf (_("[GRAPH] Colour palette analysis done in %d ms\n"), ticks2-ticks1) ;
    }
}

void gr_refresh_palette()
{
    int n ;

    if (!scr_initialized) return ;

    memset (nearest_table, 0, sizeof(nearest_table)) ;

    for (n = 0 ; n < 256 ; n++) color_list[n] = n ;
    qsort (color_list, 256, sizeof(int), compare_colors) ;

    trans_table_updated = 0 ;

    /* Actualiza la paleta */

    palette_changed = 0 ;
/*
    if (fade_step != 64 && fade_step != 0)
    {
        memcpy (vpalette, palette, sizeof(vpalette)) ;
        activate_vpalette() ;
        background_dirty = 1 ; // Temporal, hasta buscar una solucion mejor
    }
    else
    { */
        if (enable_16bits) {
            for (n = 0 ; n < 256 ; n++)
                colorequiv[n] = SDL_MapRGB (screen->format, palette[n].r, palette[n].g, palette[n].b) ;
        } else {
            SDL_SetColors (screen, palette, 0, 256) ;
        }
//    }
}

void gr_set_rgb (int color, int r, int g, int b)
{
    if (color < 0 || color > 255) return ;

    palette[color].r = r << 2 ;
    palette[color].g = g << 2 ;
    palette[color].b = b << 2 ;
    palette_changed  = 1 ;
}

/* Rutinas para guardar y recibir trozos de paleta desde un buffer */

void gr_get_colors (int color, int num, Uint8 * pal)
{
    if (num < 1 || color < 0 || color > 255) return ;
    if (color+num >= 256) num = 256-color ;

    while (num--)
    {
        *pal++ = palette[color  ].r ;
        *pal++ = palette[color  ].g ;
        *pal++ = palette[color++].b ;
    }
}

void gr_set_colors (int color, int num, Uint8 * pal)
{
    if (num < 1 || color < 0 || color > 255) return ;
    if (color+num >= 256) num = 256-color ;

    while (num--)
    {
        palette[color  ].r = *pal++ ;
        palette[color  ].g = *pal++ ;
        palette[color++].b = *pal++ ;
    }
    palette_changed = 1 ;
}