Subversion Repositories eduke32

Rev

Rev 6055 | Rev 7272 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
854 hnt_ts 1
// "Build Engine & Tools" Copyright (c) 1993-1997 Ken Silverman
2
// Ken Silverman's official web site: "http://www.advsys.net/ken"
3
// See the included license file "BUILDLIC.TXT" for license info.
4
//
5
// This file has been modified from Ken Silverman's original release
2456 hendricks2 6
// by Jonathon Fowler (jf@jonof.id.au)
854 hnt_ts 7
 
8
#include "compat.h"
9
#include "pragmas.h"
5363 hendricks2 10
#include "colmatch.h"
854 hnt_ts 11
 
12
#define MAXPALOOKUPS 256
13
 
5363 hendricks2 14
static int numshades, transratio;
15
static char const * const palettefilename = "palette.dat";
16
static uint8_t origpalookup[MAXPALOOKUPS<<8], palookup[MAXPALOOKUPS<<8], transluc[65536];
17
static uint8_t origpalette[768], palette[768];
854 hnt_ts 18
 
5363 hendricks2 19
static char getshade(char dashade, char dacol)
854 hnt_ts 20
{
2473 hendricks2 21
    int r, g, b, t;
22
    char *ptr;
854 hnt_ts 23
 
2473 hendricks2 24
    ptr = (char *)&palette[dacol*3];
5363 hendricks2 25
    t = divscale16(numshades-dashade,numshades);
2473 hendricks2 26
    r = ((ptr[0]*t+32768)>>16);
27
    g = ((ptr[1]*t+32768)>>16);
28
    b = ((ptr[2]*t+32768)>>16);
6924 terminx 29
    return(paletteGetClosestColor(r,g,b));
854 hnt_ts 30
}
31
 
5363 hendricks2 32
static char gettrans(char dat1, char dat2, int datransratio)
854 hnt_ts 33
{
2473 hendricks2 34
    int r, g, b;
35
    char *ptr, *ptr2;
854 hnt_ts 36
 
2473 hendricks2 37
    ptr = (char *)&palette[dat1*3];
38
    ptr2 = (char *)&palette[dat2*3];
39
    r = ptr[0]; r += (((ptr2[0]-r)*datransratio+128)>>8);
40
    g = ptr[1]; g += (((ptr2[1]-g)*datransratio+128)>>8);
41
    b = ptr[2]; b += (((ptr2[2]-b)*datransratio+128)>>8);
6924 terminx 42
    return(paletteGetClosestColor(r,g,b));
854 hnt_ts 43
}
44
 
45
int main(int argc, char **argv)
46
{
2473 hendricks2 47
    char col, ch;
5363 hendricks2 48
    short orignumshades;
2473 hendricks2 49
    int fil, i, j, rscale, gscale, bscale;
854 hnt_ts 50
 
2473 hendricks2 51
    ch = 13;
52
    if (argc>1) {
53
        if (argv[1][0] == '-') {
54
            if (argv[1][1] == 't') { ch = 32; puts("Updating translucency table ONLY"); }
55
            argc--;
56
            argv++;
57
        }
58
    }
854 hnt_ts 59
 
2473 hendricks2 60
    if ((argc != 3) && (argc != 6))
61
    {
2521 hendricks2 62
        Bprintf("TRANSPAL [-t]<numshades><trans#(0-inv,256-opa)><r><g><b>  by Kenneth Silverman\n");
63
        Bprintf("   Ex #1: transpal 32 170 30 59 11      (I use these values in my BUILD demo)\n");
3122 hendricks2 64
        Bprintf("                          \xc0\xc4\xc4\xc1\xc4\xc4\xc1\xc4\xc4\xc4 The RGB scales are optional\n");
2521 hendricks2 65
        Bprintf("   Ex #2: transpal 64 160\n\n");
66
        Bprintf("Once tables are generated, the optional -t switch determines what to save:\n");
67
        Bprintf("   Exclude -t to update both the shade table and transluscent table\n");
68
        Bprintf("   Include -t to update the transluscent table ONLY\n");
2473 hendricks2 69
        exit(0);
70
    }
854 hnt_ts 71
 
5363 hendricks2 72
    numshades = Batol(argv[1]);
2521 hendricks2 73
    transratio = Batol(argv[2]);
854 hnt_ts 74
 
2473 hendricks2 75
    if (argc == 6)
76
    {
2521 hendricks2 77
        rscale = Batol(argv[3]);
78
        gscale = Batol(argv[4]);
79
        bscale = Batol(argv[5]);
2473 hendricks2 80
    }
81
    else
82
    {
83
        rscale = 30;
84
        gscale = 59;
85
        bscale = 11;
86
    }
854 hnt_ts 87
 
5363 hendricks2 88
    if ((numshades < 1) || (numshades > 256))
2521 hendricks2 89
        { Bprintf("Invalid number of shades\n"); exit(0); }
2473 hendricks2 90
    if ((transratio < 0) || (transratio > 256))
2521 hendricks2 91
        { Bprintf("Invalid transluscent ratio\n"); exit(0); }
854 hnt_ts 92
 
2473 hendricks2 93
    if ((fil = Bopen(palettefilename,BO_BINARY|BO_RDONLY,BS_IREAD)) == -1)
94
    {
2521 hendricks2 95
        Bprintf("%s not found",palettefilename);
2473 hendricks2 96
        return(0);
97
    }
5363 hendricks2 98
    Bread(fil,origpalette,768);
99
    Bread(fil,&orignumshades,2); orignumshades = B_LITTLE16(orignumshades);
100
    orignumshades = min(max(orignumshades,1),256);
101
    Bread(fil,origpalookup,(int)orignumshades<<8);
2473 hendricks2 102
    Bclose(fil);
854 hnt_ts 103
 
5363 hendricks2 104
    for (int k = 0; k < 768; k++)
105
        palette[k] = origpalette[k] << 2;
854 hnt_ts 106
 
5363 hendricks2 107
    initdivtables();
108
    initfastcolorlookup_scale(rscale,gscale,bscale);
109
    initfastcolorlookup_palette(palette);
110
    initfastcolorlookup_gridvectors();
854 hnt_ts 111
 
5363 hendricks2 112
    for(i=0;i<numshades;i++)
2473 hendricks2 113
        for(j=0;j<256;j++)
114
        {
5363 hendricks2 115
            col = getshade((char)i,(char)j);
2473 hendricks2 116
            palookup[(i<<8)+j] = col;
117
        }
854 hnt_ts 118
 
2473 hendricks2 119
    for(i=0;i<256;i++)
120
        for(j=0;j<256;j++)
121
        {
122
            col = gettrans((char)i,(char)j,transratio);
123
            transluc[(i<<8)+j] = col;
124
        }
854 hnt_ts 125
 
2473 hendricks2 126
    if (ch == 13)
127
    {
128
        short s;
129
        if ((fil = Bopen(palettefilename,BO_BINARY|BO_TRUNC|BO_CREAT|BO_WRONLY,BS_IREAD|BS_IWRITE)) == -1)
2521 hendricks2 130
            { Bprintf("Couldn't save file %s",palettefilename); return(0); }
5363 hendricks2 131
        Bwrite(fil,origpalette,768);
132
        s = B_LITTLE16(numshades); Bwrite(fil,&s,2);
133
        Bwrite(fil,palookup,numshades<<8);
2473 hendricks2 134
        Bwrite(fil,transluc,65536);
135
        Bclose(fil);
2521 hendricks2 136
        Bprintf("Shade table AND transluscent table updated\n");
2473 hendricks2 137
    }
138
    else if (ch == 32)
139
    {
140
        short s;
141
        if ((fil = Bopen(palettefilename,BO_BINARY|BO_TRUNC|BO_CREAT|BO_WRONLY,BS_IREAD|BS_IWRITE)) == -1)
2521 hendricks2 142
            { Bprintf("Couldn't save file %s",palettefilename); return(0); }
5363 hendricks2 143
        Bwrite(fil,origpalette,768);
144
        s = B_LITTLE16(orignumshades); Bwrite(fil,&s,2);
145
        Bwrite(fil,origpalookup,(int)orignumshades<<8);
2473 hendricks2 146
        Bwrite(fil,transluc,65536);
147
        Bclose(fil);
2521 hendricks2 148
        Bprintf("Transluscent table updated\n");
2473 hendricks2 149
    }
150
    else
2521 hendricks2 151
        Bprintf("Palette file wasn't touched\n");
854 hnt_ts 152
 
2473 hendricks2 153
    return 0;
854 hnt_ts 154
}