Subversion Repositories eduke32

Rev

Rev 8315 | Rev 8489 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 8315 Rev 8488
Line 17... Line 17...
17
static uint8_t const * colmatch_palette;
17
static uint8_t const * colmatch_palette;
18
18
19
#define pow2char(x) (1u << (x))
19
#define pow2char(x) (1u << (x))
20
20
21
//
21
//
22
// initfastcolorlookup
22
// paletteInitClosestColor
23
//
23
//
24
void initfastcolorlookup_scale(int32_t rscale, int32_t gscale, int32_t bscale)
24
void paletteInitClosestColorScale(int32_t rscale, int32_t gscale, int32_t bscale)
25
{
25
{
26
    int32_t j = 0;
26
    int32_t j = 0;
27
    for (bssize_t i=256; i>=0; i--)
27
    for (bssize_t i=256; i>=0; i--)
28
    {
28
    {
29
        //j = (i-64)*(i-64);
29
        //j = (i-64)*(i-64);
Line 31... Line 31...
31
        gdist[i] = gdist[FASTPALCOLDEPTH*2-i] = j*gscale;
31
        gdist[i] = gdist[FASTPALCOLDEPTH*2-i] = j*gscale;
32
        bdist[i] = bdist[FASTPALCOLDEPTH*2-i] = j*bscale;
32
        bdist[i] = bdist[FASTPALCOLDEPTH*2-i] = j*bscale;
33
        j += FASTPALRGBDIST-(i<<1);
33
        j += FASTPALRGBDIST-(i<<1);
34
    }
34
    }
35
}
35
}
36
void initfastcolorlookup_palette(uint8_t const * const pal)
36
void paletteInitClosestColorMap(uint8_t const * const pal)
37
{
37
{
38
    Bmemset(colhere,0,sizeof(colhere));
38
    Bmemset(colhere,0,sizeof(colhere));
39
    Bmemset(colhead,0,sizeof(colhead));
39
    Bmemset(colhead,0,sizeof(colhead));
40
40
41
    colmatch_palette = pal;
41
    colmatch_palette = pal;
Line 49... Line 49...
49
        if (colhere[j>>3]&pow2char(j&7)) colnext[i] = colhead[j]; else colnext[i] = -1;
49
        if (colhere[j>>3]&pow2char(j&7)) colnext[i] = colhead[j]; else colnext[i] = -1;
50
        colhead[j] = i;
50
        colhead[j] = i;
51
        colhere[j>>3] |= pow2char(j&7);
51
        colhere[j>>3] |= pow2char(j&7);
52
    }
52
    }
53
53
54
    getclosestcol_flush();
54
    paletteFlushClosestColor();
55
}
55
}
56
void initfastcolorlookup_gridvectors(void)
56
void paletteInitClosestColorGrid(void)
57
{
57
{
58
    int i = 0;
58
    int i = 0;
59
    int32_t x, y, z;
59
    int32_t x, y, z;
60
    for (x=-FASTPALGRIDSIZ*FASTPALGRIDSIZ; x<=FASTPALGRIDSIZ*FASTPALGRIDSIZ; x+=FASTPALGRIDSIZ*FASTPALGRIDSIZ)
60
    for (x=-FASTPALGRIDSIZ*FASTPALGRIDSIZ; x<=FASTPALGRIDSIZ*FASTPALGRIDSIZ; x+=FASTPALGRIDSIZ*FASTPALGRIDSIZ)
61
        for (y=-FASTPALGRIDSIZ; y<=FASTPALGRIDSIZ; y+=FASTPALGRIDSIZ)
61
        for (y=-FASTPALGRIDSIZ; y<=FASTPALGRIDSIZ; y+=FASTPALGRIDSIZ)
Line 69... Line 69...
69
        coldist[i] = FASTPALCOLDIST-i;
69
        coldist[i] = FASTPALCOLDIST-i;
70
}
70
}
71
71
72
#define COLRESULTSIZ 4096
72
#define COLRESULTSIZ 4096
73
73
74
static uint32_t getclosestcol_results[COLRESULTSIZ];
74
static uint32_t colmatchresults[COLRESULTSIZ];
75
static int32_t numclosestcolresults;
75
static int32_t numcolmatchresults;
76
76
77
void getclosestcol_flush(void)
77
void paletteFlushClosestColor(void)
78
{
78
{
79
    Bmemset(getclosestcol_results, 0, COLRESULTSIZ * sizeof(uint32_t));
79
    Bmemset(colmatchresults, 0, COLRESULTSIZ * sizeof(uint32_t));
80
    numclosestcolresults = 0;
80
    numcolmatchresults = 0;
81
}
81
}
82
82
83
// Finds a color index in [0 .. lastokcol] closest to (r, g, b).
83
// Finds a color index in [0 .. lastokcol] closest to (r, g, b).
84
// <lastokcol> must be in [0 .. 255].
84
// <lastokcol> must be in [0 .. 255].
85
int32_t getclosestcol_lim(int32_t const r, int32_t const g, int32_t const b, int32_t const lastokcol)
85
int32_t paletteGetClosestColorUpToIndex(int32_t const r, int32_t const g, int32_t const b, int32_t const lastokcol)
86
{
86
{
87
#ifdef DEBUGGINGAIDS
87
#ifdef DEBUGGINGAIDS
88
    Bassert(lastokcol >= 0 && lastokcol <= 255);
88
    Bassert(lastokcol >= 0 && lastokcol <= 255);
89
#endif
89
#endif
90
90
91
    uint32_t const col = r | (g<<8) | (b<<16);
91
    uint32_t const col = r | (g<<8) | (b<<16);
92
92
93
    int mindist = -1;
93
    int mindist = -1;
94
94
95
    int const k = (numclosestcolresults > COLRESULTSIZ) ? COLRESULTSIZ : numclosestcolresults;
95
    int const k = (numcolmatchresults > COLRESULTSIZ) ? COLRESULTSIZ : numcolmatchresults;
96
96
97
    if (!numclosestcolresults) goto skip;
97
    if (!numcolmatchresults) goto skip;
98
98
99
    if (col == (getclosestcol_results[(numclosestcolresults-1) & (COLRESULTSIZ-1)] & 0x00ffffff))
99
    if (col == (colmatchresults[(numcolmatchresults-1) & (COLRESULTSIZ-1)] & 0x00ffffff))
100
        return getclosestcol_results[(numclosestcolresults-1) & (COLRESULTSIZ-1)]>>24;
100
        return colmatchresults[(numcolmatchresults-1) & (COLRESULTSIZ-1)]>>24;
101
101
102
    int i;
102
    int i;
103
103
104
    for (i = 0; i <= k-4; i+=4)
104
    for (i = 0; i <= k-4; i+=4)
105
    {
105
    {
106
        if (col == (getclosestcol_results[i]   & 0x00ffffff)) { mindist = i; break; }
106
        if (col == (colmatchresults[i]   & 0x00ffffff)) { mindist = i; break; }
107
        if (col == (getclosestcol_results[i+1] & 0x00ffffff)) { mindist = i+1; break; }
107
        if (col == (colmatchresults[i+1] & 0x00ffffff)) { mindist = i+1; break; }
108
        if (col == (getclosestcol_results[i+2] & 0x00ffffff)) { mindist = i+2; break; }
108
        if (col == (colmatchresults[i+2] & 0x00ffffff)) { mindist = i+2; break; }
109
        if (col == (getclosestcol_results[i+3] & 0x00ffffff)) { mindist = i+3; break; }
109
        if (col == (colmatchresults[i+3] & 0x00ffffff)) { mindist = i+3; break; }
110
    }
110
    }
111
111
112
    if (mindist == -1)
112
    if (mindist == -1)
113
    for (; i < k; i++)
113
    for (; i < k; i++)
114
        if (col == (getclosestcol_results[i] & 0x00ffffff)) { mindist = i; break; }
114
        if (col == (colmatchresults[i] & 0x00ffffff)) { mindist = i; break; }
115
115
116
    if (mindist != -1 && getclosestcol_results[mindist]>>24 < (unsigned)lastokcol)
116
    if (mindist != -1 && colmatchresults[mindist]>>24 < (unsigned)lastokcol)
117
        return getclosestcol_results[mindist]>>24;
117
        return colmatchresults[mindist]>>24;
118
118
119
skip:
119
skip:
120
    i = getclosestcol_nocache_lim(r, g, b, lastokcol);
120
    i = paletteGetClosestColorUpToIndexNoCache(r, g, b, lastokcol);
121
    getclosestcol_results[numclosestcolresults++ & (COLRESULTSIZ-1)] = col | (i << 24);
121
    colmatchresults[numcolmatchresults++ & (COLRESULTSIZ-1)] = col | (i << 24);
122
    return i;
122
    return i;
123
}
123
}
124
124
125
int32_t getclosestcol_nocache_lim(int32_t r, int32_t g, int32_t b, int32_t const lastokcol)
125
int32_t paletteGetClosestColorUpToIndexNoCache(int32_t r, int32_t g, int32_t b, int32_t const lastokcol)
126
{
126
{
127
#ifdef DEBUGGINGAIDS
127
#ifdef DEBUGGINGAIDS
128
    Bassert(lastokcol >= 0 && lastokcol <= 255);
128
    Bassert(lastokcol >= 0 && lastokcol <= 255);
129
#endif
129
#endif
130
130