林伟  

 

256色调色板的匹配处理


   有一天一个朋友问我如何使几张256色图片的条色板得到统一?如何在256色模式中实现Alpha混色,明暗 等处理。问题很经典,我长期从事SDK研究所以这个是课题中不可少的一部分。结论在于不仅解决了调色板匹 配问题。而且同时给出了一个相对目标调色板的查找表 char rgb_map[32][32][32]的查找表(RGB->256), 相应RGB色可以在这张表里通过rgb_map[r][g][b]查到最相近的一个颜色值,而且要筛选图片的颜色出现频 率来缩小一个图片的调色板数,如此再进行调色板合成。生成一个较为通用的调色板最终根据rgb_map进行N 张图片的色彩匹配。我尽量将文章写短,节省大家的时间:

问题的核心函数就是在指定调色板中查找一个与给定颜色(RGB值)最相近的一个色彩号。算法有两种:
1. 将调色板每个RGB值与给定RGB值作对应求差后象加,在得出的一系列数值中找出最小那个便是最相近的了
2. 这种算法国际上颇为流行,程序给出如下:

static unsigned col_diff[3*128]={0,0,0};
static void bestfit_init()
{
    int i;
    for (i=1; i<64; i++)
    {
        int k = i * i;
        col_diff[0 +i] = col_diff[0 +128-i] = k * (59 * 59);
        col_diff[128+i] = col_diff[128+128-i] = k * (30 * 30);
        col_diff[256+i] = col_diff[256+128-i] = k * (11 * 11);
    }
}

uchar lpBestfitColor(RGB *pal,short r,short g,short b)
{
    int i, coldiff, lowest, bestfit;
    if (col_diff[1] == 0)
        bestfit_init();
    bestfit = 0;
    lowest = INT_MAX;
    if ((r == 63) && (g == 0) && (b == 63))
        i = 0;
    else
        i = 1;

    while (i<256)
    {
        RGB *rgb = &pal[i];
        coldiff = (col_diff + 0) [ (rgb->g - g) & 0x7F ];
        if (coldiff < lowest)
        {
            coldiff += (col_diff + 128) [ (rgb->r - r) & 0x7F ];
            if (coldiff < lowest)
            {
                coldiff += (col_diff + 256) [ (rgb->b - b) & 0x7F ];
                if (coldiff < lowest)
                {
                    bestfit = rgb - pal; /* faster than `bestfit = i;' */
                    if (coldiff == 0)
                        return bestfit;
                    lowest = coldiff;
                }
            }
        }
        i++;
    }

    return bestfit;


有了这个函数的功能,问题就解决了。下面我们生成一张 RGB->256的查找表,由于256色发色数不多。故只用五位代码来表示

struct RGB_MAP
{
    unsigned char data[32][32][32];
};

void lpCreateRgbTable(RGB_MAP *table, RGB *pal, void (*callback)(int pos))
{
    int i,r,g,b,k;
    for (r=0,i=0,k=0;r<32;r++) for (g=0;g<32;g++)
    {
        for (b=0;b<32;b++) 
        table->data[r][g][b]=lpBestfitColor(pal,r<<1,g<<1,b<<1);
        if (++i>=4)
        {
            i=0;
            if (callback) (*callback)(k++);
        }
    }
}

有了这RGB->256的表,什么问题都解决了。
优化问题:在50%的Alpha混色还可以生成一张二维表进行更有效的优化。

struct COLOR_MAP
{
    unsigned char data[256][256];
};

混合时只要 color=color_map.data[color1][color2]; 这么简单。写成汇编比16位色的50%混合快许多
另外运用COLOR_MAP还可以生成亮度表,color=light_map.data[source_color][light];如此编程也是相
当简单的。下面是生成亮度查找表的程序:

void lpCreateLightTable(COLOR_MAP *table, RGB *pal, int r, int g, int b, void (*callback)(int pos))
{
    int x, y;
    RGB c;

    for (x=0; x<256; x++)
    {
        for (y=0; y<256; y++)
        {
            c.r = (r * (255 - x) / 255) + ((int)pal[y].r * x / 255);
            c.g = (g * (255 - x) / 255) + ((int)pal[y].g * x / 255);
            c.b = (b * (255 - x) / 255) + ((int)pal[y].b * x / 255);

            if (rgb_map)
                table->data[x][y] = rgb_map->data[c.r>>1][c.g>>1][c.b>>1];
            else
                table->data[x][y] = lpBestfitColor(pal, c.r, c.g, c.b);
        }

        if (callback)
            (*callback)(x);
    }
}

下面是一张覆盖面广的调色板,我由《仙剑》中的调色板加以改动得来,经过长期使用。我觉得大宇真是
经典因为这几乎是最好的调色板(起码我没有找到比它更具覆盖性的)一般匹配其他图片可以达到90%的近似
效果,请将下面代码抄到你的程序中:

static RGB def_pal[256*10]={
0,0,0,6,6,6,10,10,10,14,14,14,18,18,18,22,22,22,26,26,26,30,30,30,34,34,34,38,38,38,42,42,42,
46,46,46,50,50,50,54,54,54,59,59,59,63,63,63,20,0,0,23,0,0,28,0,0,33,1,1,38,2,2,47,4,3,54,6, 
5,63,0,0,63,0,0,63,14,11,63,18,15,63,22,19,63,22,18,63,32,28,63,37,33,63,43,39,18,5,2,21,6,3,
24,6,3,27,6,3,31,10,4,36,15,7,40,20,9,44,25,11,48,30,15,53,36,18,56,41,21,60,46,24,63,50,28,
63,55,33,63,60,39,63,62,44,12,6,0,22,15,0,32,25,0,41,34,1,48,43,1,57,50,1,59,56,1,62,62,1,62,
62,21,63,63,0,63,63,10,63,63,19,63,63,28,63,63,41,63,63,49,63,63,60,5,3,2,7,5,3,10,7,5,13,10,
7,16,13,9,18,16,11,21,19,14,24,22,16,27,25,19,29,28,21,32,31,24,35,34,28,38,37,31,41,40,36,
45,44,40,49,46,43,0,0,15,0,0,19,1,1,27,2,2,32,3,3,37,2,2,41,3,3,46,0,3,51,0,0,58,0,0,63,7,7,
63,14,14,63,21,21,63,28,28,63,38,38,63,42,42,63,7,4,14,9,5,17,11,6,20,13,8,23,15,10,26,18,12,
29,21,14,32,24,17,35,27,20,38,31,23,41,34,27,44,38,31,47,42,35,50,46,40,53,50,44,56,54,49,59,
5,9,10,7,11,13,9,14,16,11,17,19,13,20,22,16,23,25,19,26,28,22,30,32,26,33,35,30,37,39,33,40,
42,38,44,46,42,48,50,47,52,53,52,56,57,57,60,61,0,4,2,0,8,5,0,11,8,1,15,11,2,19,15,4,23,19,6,
26,22,8,30,26,11,34,29,13,38,33,17,42,37,21,46,41,26,50,45,31,54,49,36,58,52,42,62,57,23,10,
6,28,14,8,33,18,10,36,23,13,40,28,16,43,32,19,45,33,21,47,33,24,49,33,27,50,36,30,52,39,33,
54,42,37,55,45,40,57,48,43,58,51,47,60,54,50,12,4,0,16,6,0,21,8,1,24,10,2,27,12,3,30,15,6,33,
18,8,36,21,11,39,25,14,42,29,17,45,33,21,48,37,25,51,41,30,55,46,35,59,50,41,63,56,48,9,3,1,
12,5,2,15,7,3,18,9,4,22,13,7,25,16,9,28,19,12,32,23,15,35,28,18,39,32,22,42,36,27,46,41,31,
49,45,36,53,50,42,57,55,48,61,60,55,0,7,0,0,9,0,1,12,0,2,15,1,4,19,2,6,24,3,8,31,5,11,39,7,
14,45,9,0,49,0,6,49,0,11,49,0,19,49,19,26,49,22,32,49,26,28,49,31,0,6,23,0,8,27,1,10,31,3,12,
35,5,15,40,7,17,44,10,20,48,13,23,50,17,27,52,21,31,55,25,35,56,30,39,58,35,43,59,39,47,60,
44,51,61,50,55,63,9,5,3,12,7,4,15,10,6,18,13,8,22,17,11,25,20,13,28,23,16,31,27,19,34,30,22,
37,34,26,40,37,30,44,41,34,47,45,38,50,49,42,53,52,46,56,55,50,0,0,0,19,23,9,15,22,7,25,30,
21,24,32,0,13,13,0,21,25,11,25,26,13,13,13,63,0,38,38,29,28,15,19,26,15,0,0,0,36,36,0,50,50,
0,63,63,0 };