Subversion Repositories eduke32

Rev

Rev 8315 | Rev 8489 | 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)
7806 terminx 9
static char colhere[((FASTPALGRIDSIZ+2)*(FASTPALGRIDSIZ+2)*(FASTPALGRIDSIZ+2)+7)>>3];
5359 hendricks2 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
//
8488 hendricks2 22
// paletteInitClosestColor
5359 hendricks2 23
//
8488 hendricks2 24
void paletteInitClosestColorScale(int32_t rscale, int32_t gscale, int32_t bscale)
5359 hendricks2 25
{
26
    int32_t j = 0;
5829 terminx 27
    for (bssize_t i=256; i>=0; i--)
5359 hendricks2 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
}
8488 hendricks2 36
void paletteInitClosestColorMap(uint8_t const * const pal)
5359 hendricks2 37
{
38
    Bmemset(colhere,0,sizeof(colhere));
39
    Bmemset(colhead,0,sizeof(colhead));
40
 
41
    colmatch_palette = pal;
42
 
5540 terminx 43
    char const *pal1 = (char const *)&pal[768-3];
5829 terminx 44
    for (bssize_t i=255; i>=0; i--,pal1-=3)
5359 hendricks2 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
 
8488 hendricks2 54
    paletteFlushClosestColor();
5359 hendricks2 55
}
8488 hendricks2 56
void paletteInitClosestColorGrid(void)
5359 hendricks2 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
 
8488 hendricks2 74
static uint32_t colmatchresults[COLRESULTSIZ];
75
static int32_t numcolmatchresults;
5359 hendricks2 76
 
8488 hendricks2 77
void paletteFlushClosestColor(void)
5359 hendricks2 78
{
8488 hendricks2 79
    Bmemset(colmatchresults, 0, COLRESULTSIZ * sizeof(uint32_t));
80
    numcolmatchresults = 0;
5359 hendricks2 81
}
82
 
83
// Finds a color index in [0 .. lastokcol] closest to (r, g, b).
84
// <lastokcol> must be in [0 .. 255].
8488 hendricks2 85
int32_t paletteGetClosestColorUpToIndex(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
 
8488 hendricks2 95
    int const k = (numcolmatchresults > COLRESULTSIZ) ? COLRESULTSIZ : numcolmatchresults;
5359 hendricks2 96
 
8488 hendricks2 97
    if (!numcolmatchresults) goto skip;
5359 hendricks2 98
 
8488 hendricks2 99
    if (col == (colmatchresults[(numcolmatchresults-1) & (COLRESULTSIZ-1)] & 0x00ffffff))
100
        return colmatchresults[(numcolmatchresults-1) & (COLRESULTSIZ-1)]>>24;
5359 hendricks2 101
 
102
    int i;
103
 
8315 hendricks2 104
    for (i = 0; i <= k-4; i+=4)
5359 hendricks2 105
    {
8488 hendricks2 106
        if (col == (colmatchresults[i]   & 0x00ffffff)) { mindist = i; break; }
107
        if (col == (colmatchresults[i+1] & 0x00ffffff)) { mindist = i+1; break; }
108
        if (col == (colmatchresults[i+2] & 0x00ffffff)) { mindist = i+2; break; }
109
        if (col == (colmatchresults[i+3] & 0x00ffffff)) { mindist = i+3; break; }
5359 hendricks2 110
    }
111
 
112
    if (mindist == -1)
113
    for (; i < k; i++)
8488 hendricks2 114
        if (col == (colmatchresults[i] & 0x00ffffff)) { mindist = i; break; }
5359 hendricks2 115
 
8488 hendricks2 116
    if (mindist != -1 && colmatchresults[mindist]>>24 < (unsigned)lastokcol)
117
        return colmatchresults[mindist]>>24;
5359 hendricks2 118
 
119
skip:
8488 hendricks2 120
    i = paletteGetClosestColorUpToIndexNoCache(r, g, b, lastokcol);
121
    colmatchresults[numcolmatchresults++ & (COLRESULTSIZ-1)] = col | (i << 24);
5376 hendricks2 122
    return i;
123
}
5359 hendricks2 124
 
8488 hendricks2 125
int32_t paletteGetClosestColorUpToIndexNoCache(int32_t r, int32_t g, int32_t b, int32_t const lastokcol)
5376 hendricks2 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
 
5829 terminx 147
    for (bssize_t k=26; k>=0; k--)
5359 hendricks2 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
        {
5540 terminx 158
            char const * const pal1 = (char const *)&colmatch_palette[i*3];
5359 hendricks2 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
 
5829 terminx 176
    for (bssize_t i = 0; i < lastokcol; ++i)
5359 hendricks2 177
    {
5540 terminx 178
        char const * const pal1 = (char const *)&colmatch_palette[i*3];
5359 hendricks2 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
}