Subversion Repositories eduke32

Rev

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

Rev Author Line No. Line
5359 hendricks2 1
 
2
#include "colmatch.h"
3
 
4
#define FASTPALCOLDEPTH 256
5
#define FASTPALRIGHTSHIFT 3
6
#define FASTPALRGBDIST (FASTPALCOLDEPTH*2+1)
7
static int32_t rdist[FASTPALRGBDIST], gdist[FASTPALRGBDIST], bdist[FASTPALRGBDIST];
8
#define FASTPALGRIDSIZ (FASTPALCOLDEPTH>>FASTPALRIGHTSHIFT)
9
static char colhere[((FASTPALGRIDSIZ+2)*(FASTPALGRIDSIZ+2)*(FASTPALGRIDSIZ+2))>>3];
10
static char colhead[(FASTPALGRIDSIZ+2)*(FASTPALGRIDSIZ+2)*(FASTPALGRIDSIZ+2)];
11
static int32_t colnext[256];
12
#define FASTPALCOLDIST (1<<FASTPALRIGHTSHIFT)
13
#define FASTPALCOLDISTMASK (FASTPALCOLDIST-1)
14
static uint8_t coldist[FASTPALCOLDIST];
15
static int32_t colscan[27];
16
 
17
static uint8_t const * colmatch_palette;
18
 
19
#define pow2char(x) (1u << (x))
20
 
21
//
22
// initfastcolorlookup
23
//
24
void initfastcolorlookup_scale(int32_t rscale, int32_t gscale, int32_t bscale)
25
{
26
    int32_t j = 0;
27
    for (int i=256; i>=0; i--)
28
    {
29
        //j = (i-64)*(i-64);
30
        rdist[i] = rdist[FASTPALCOLDEPTH*2-i] = j*rscale;
31
        gdist[i] = gdist[FASTPALCOLDEPTH*2-i] = j*gscale;
32
        bdist[i] = bdist[FASTPALCOLDEPTH*2-i] = j*bscale;
33
        j += FASTPALRGBDIST-(i<<1);
34
    }
35
}
36
void initfastcolorlookup_palette(uint8_t const * const pal)
37
{
38
    Bmemset(colhere,0,sizeof(colhere));
39
    Bmemset(colhead,0,sizeof(colhead));
40
 
41
    colmatch_palette = pal;
42
 
43
    char const *pal1 = (char *)&pal[768-3];
44
    for (int i=255; i>=0; i--,pal1-=3)
45
    {
46
        int32_t const j = (pal1[0]>>FASTPALRIGHTSHIFT)*FASTPALGRIDSIZ*FASTPALGRIDSIZ
47
            + (pal1[1]>>FASTPALRIGHTSHIFT)*FASTPALGRIDSIZ + (pal1[2]>>FASTPALRIGHTSHIFT)
48
            + FASTPALGRIDSIZ*FASTPALGRIDSIZ + FASTPALGRIDSIZ + 1;
49
        if (colhere[j>>3]&pow2char(j&7)) colnext[i] = colhead[j]; else colnext[i] = -1;
50
        colhead[j] = i;
51
        colhere[j>>3] |= pow2char(j&7);
52
    }
5367 hendricks2 53
 
54
    getclosestcol_flush();
5359 hendricks2 55
}
56
void initfastcolorlookup_gridvectors(void)
57
{
58
    int i = 0;
59
    int32_t x, y, z;
60
    for (x=-FASTPALGRIDSIZ*FASTPALGRIDSIZ; x<=FASTPALGRIDSIZ*FASTPALGRIDSIZ; x+=FASTPALGRIDSIZ*FASTPALGRIDSIZ)
61
        for (y=-FASTPALGRIDSIZ; y<=FASTPALGRIDSIZ; y+=FASTPALGRIDSIZ)
62
            for (z=-1; z<=1; z++)
63
                colscan[i++] = x+y+z;
64
    i = colscan[13]; colscan[13] = colscan[26]; colscan[26] = i;
65
 
66
    for (i = 0; i < FASTPALCOLDIST/2; i++)
67
        coldist[i] = i;
68
    for (; i < FASTPALCOLDIST; i++)
69
        coldist[i] = FASTPALCOLDIST-i;
70
}
71
 
72
#define COLRESULTSIZ 4096
73
 
74
static uint32_t getclosestcol_results[COLRESULTSIZ];
75
static int32_t numclosestcolresults;
76
 
77
void getclosestcol_flush(void)
78
{
79
    Bmemset(getclosestcol_results, 0, COLRESULTSIZ * sizeof(uint32_t));
80
    numclosestcolresults = 0;
81
}
82
 
83
// Finds a color index in [0 .. lastokcol] closest to (r, g, b).
84
// <lastokcol> must be in [0 .. 255].
5376 hendricks2 85
int32_t getclosestcol_lim(int32_t const r, int32_t const g, int32_t const b, int32_t const lastokcol)
5359 hendricks2 86
{
87
#ifdef DEBUGGINGAIDS
88
    Bassert(lastokcol >= 0 && lastokcol <= 255);
89
#endif
90
 
91
    uint32_t const col = r | (g<<8) | (b<<16);
92
 
93
    int mindist = -1;
94
 
95
    int const k = (numclosestcolresults > COLRESULTSIZ) ? (COLRESULTSIZ-4) : (numclosestcolresults-4);
96
 
97
    if (!numclosestcolresults) goto skip;
98
 
99
    if (col == (getclosestcol_results[(numclosestcolresults-1) & (COLRESULTSIZ-1)] & 0x00ffffff))
100
        return getclosestcol_results[(numclosestcolresults-1) & (COLRESULTSIZ-1)]>>24;
101
 
102
    int i;
103
 
104
    for (i = 0; i < k+4; i+=4)
105
    {
106
        if (col == (getclosestcol_results[i]   & 0x00ffffff)) { mindist = i; break; }
107
        if (col == (getclosestcol_results[i+1] & 0x00ffffff)) { mindist = i+1; break; }
108
        if (col == (getclosestcol_results[i+2] & 0x00ffffff)) { mindist = i+2; break; }
109
        if (col == (getclosestcol_results[i+3] & 0x00ffffff)) { mindist = i+3; break; }
110
    }
111
 
112
    if (mindist == -1)
113
    for (; i < k; i++)
114
        if (col == (getclosestcol_results[i] & 0x00ffffff)) { mindist = i; break; }
115
 
116
    if (mindist != -1 && getclosestcol_results[mindist]>>24 < (unsigned)lastokcol)
117
        return getclosestcol_results[mindist]>>24;
118
 
119
skip:
5376 hendricks2 120
    i = getclosestcol_nocache_lim(r, g, b, lastokcol);
121
    getclosestcol_results[numclosestcolresults++ & (COLRESULTSIZ-1)] = col | (i << 24);
122
    return i;
123
}
5359 hendricks2 124
 
5376 hendricks2 125
int32_t getclosestcol_nocache_lim(int32_t r, int32_t g, int32_t b, int32_t const lastokcol)
126
{
127
#ifdef DEBUGGINGAIDS
128
    Bassert(lastokcol >= 0 && lastokcol <= 255);
129
#endif
130
 
131
    int const j = (r>>FASTPALRIGHTSHIFT)*FASTPALGRIDSIZ*FASTPALGRIDSIZ
132
        + (g>>FASTPALRIGHTSHIFT)*FASTPALGRIDSIZ + (b>>FASTPALRIGHTSHIFT)
133
        + FASTPALGRIDSIZ*FASTPALGRIDSIZ
134
        + FASTPALGRIDSIZ + 1;
135
 
5359 hendricks2 136
    int const minrdist = rdist[coldist[r&FASTPALCOLDISTMASK]+FASTPALCOLDEPTH];
137
    int const mingdist = gdist[coldist[g&FASTPALCOLDISTMASK]+FASTPALCOLDEPTH];
138
    int const minbdist = bdist[coldist[b&FASTPALCOLDISTMASK]+FASTPALCOLDEPTH];
139
 
5376 hendricks2 140
    int mindist = min(minrdist, mingdist);
5359 hendricks2 141
    mindist = min(mindist, minbdist) + 1;
142
 
143
    r = FASTPALCOLDEPTH-r, g = FASTPALCOLDEPTH-g, b = FASTPALCOLDEPTH-b;
144
 
145
    int retcol = -1;
146
 
147
    for (int k=26; k>=0; k--)
148
    {
5376 hendricks2 149
        int i = colscan[k]+j;
5359 hendricks2 150
 
151
        if ((colhere[i>>3]&pow2char(i&7)) == 0)
152
            continue;
153
 
154
        i = colhead[i];
155
 
156
        do
157
        {
158
            char const * const pal1 = (char *)&colmatch_palette[i*3];
159
            int dist = gdist[pal1[1]+g];
160
 
161
            if (dist >= mindist || i > lastokcol) continue;
162
            if ((dist += rdist[pal1[0]+r]) >= mindist) continue;
163
            if ((dist += bdist[pal1[2]+b]) >= mindist) continue;
164
 
165
            mindist = dist;
166
            retcol = i;
167
        }
168
        while ((i = colnext[i]) >= 0);
169
    }
170
 
171
    if (retcol >= 0)
172
        return retcol;
173
 
174
    mindist = INT32_MAX;
175
 
5376 hendricks2 176
    for (int i = 0; i < lastokcol; ++i)
5359 hendricks2 177
    {
178
        char const * const pal1 = (char *)&colmatch_palette[i*3];
179
        int dist = gdist[pal1[1]+g];
180
 
181
        if (dist >= mindist) continue;
182
        if ((dist += rdist[pal1[0]+r]) >= mindist) continue;
183
        if ((dist += bdist[pal1[2]+b]) >= mindist) continue;
184
 
185
        mindist = dist;
186
        retcol = i;
187
    }
188
 
189
    return retcol;
190
}