Subversion Repositories eduke32

Rev

Rev 5367 | Rev 5376 | 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].
85
int32_t getclosestcol_lim(int32_t r, int32_t g, int32_t b, int32_t lastokcol)
86
{
87
    const int j = (r>>FASTPALRIGHTSHIFT)*FASTPALGRIDSIZ*FASTPALGRIDSIZ
88
        + (g>>FASTPALRIGHTSHIFT)*FASTPALGRIDSIZ + (b>>FASTPALRIGHTSHIFT)
89
        + FASTPALGRIDSIZ*FASTPALGRIDSIZ
90
        + FASTPALGRIDSIZ + 1;
91
 
92
#ifdef DEBUGGINGAIDS
93
    Bassert(lastokcol >= 0 && lastokcol <= 255);
94
#endif
95
 
96
    uint32_t const col = r | (g<<8) | (b<<16);
97
 
98
    int mindist = -1;
99
 
100
    int const k = (numclosestcolresults > COLRESULTSIZ) ? (COLRESULTSIZ-4) : (numclosestcolresults-4);
101
 
102
    if (!numclosestcolresults) goto skip;
103
 
104
    if (col == (getclosestcol_results[(numclosestcolresults-1) & (COLRESULTSIZ-1)] & 0x00ffffff))
105
        return getclosestcol_results[(numclosestcolresults-1) & (COLRESULTSIZ-1)]>>24;
106
 
107
    int i;
108
 
109
    for (i = 0; i < k+4; i+=4)
110
    {
111
        if (col == (getclosestcol_results[i]   & 0x00ffffff)) { mindist = i; break; }
112
        if (col == (getclosestcol_results[i+1] & 0x00ffffff)) { mindist = i+1; break; }
113
        if (col == (getclosestcol_results[i+2] & 0x00ffffff)) { mindist = i+2; break; }
114
        if (col == (getclosestcol_results[i+3] & 0x00ffffff)) { mindist = i+3; break; }
115
    }
116
 
117
    if (mindist == -1)
118
    for (; i < k; i++)
119
        if (col == (getclosestcol_results[i] & 0x00ffffff)) { mindist = i; break; }
120
 
121
    if (mindist != -1 && getclosestcol_results[mindist]>>24 < (unsigned)lastokcol)
122
        return getclosestcol_results[mindist]>>24;
123
 
124
skip:
125
    getclosestcol_results[numclosestcolresults & (COLRESULTSIZ-1)] = col;
126
 
127
    int const minrdist = rdist[coldist[r&FASTPALCOLDISTMASK]+FASTPALCOLDEPTH];
128
    int const mingdist = gdist[coldist[g&FASTPALCOLDISTMASK]+FASTPALCOLDEPTH];
129
    int const minbdist = bdist[coldist[b&FASTPALCOLDISTMASK]+FASTPALCOLDEPTH];
130
 
131
    mindist = min(minrdist, mingdist);
132
    mindist = min(mindist, minbdist) + 1;
133
 
134
    r = FASTPALCOLDEPTH-r, g = FASTPALCOLDEPTH-g, b = FASTPALCOLDEPTH-b;
135
 
136
    int retcol = -1;
137
 
138
    for (int k=26; k>=0; k--)
139
    {
140
        i = colscan[k]+j;
141
 
142
        if ((colhere[i>>3]&pow2char(i&7)) == 0)
143
            continue;
144
 
145
        i = colhead[i];
146
 
147
        do
148
        {
149
            char const * const pal1 = (char *)&colmatch_palette[i*3];
150
            int dist = gdist[pal1[1]+g];
151
 
152
            if (dist >= mindist || i > lastokcol) continue;
153
            if ((dist += rdist[pal1[0]+r]) >= mindist) continue;
154
            if ((dist += bdist[pal1[2]+b]) >= mindist) continue;
155
 
156
            mindist = dist;
157
            retcol = i;
158
        }
159
        while ((i = colnext[i]) >= 0);
160
    }
161
 
162
    if (retcol >= 0)
163
    {
164
        getclosestcol_results[numclosestcolresults++ & (COLRESULTSIZ-1)] |= retcol<<24;
165
        return retcol;
166
    }
167
 
168
    mindist = INT32_MAX;
169
 
5371 hendricks2 170
    for (i = 0; i < lastokcol; ++i)
5359 hendricks2 171
    {
172
        char const * const pal1 = (char *)&colmatch_palette[i*3];
173
        int dist = gdist[pal1[1]+g];
174
 
175
        if (dist >= mindist) continue;
176
        if ((dist += rdist[pal1[0]+r]) >= mindist) continue;
177
        if ((dist += bdist[pal1[2]+b]) >= mindist) continue;
178
 
179
        mindist = dist;
180
        retcol = i;
181
    }
182
 
183
    getclosestcol_results[numclosestcolresults++ & (COLRESULTSIZ-1)] |= retcol<<24;
184
    return retcol;
185
}