Untitled
unknown
c_cpp
4 years ago
2.1 kB
23
Indexable
color allColor[32768];
for (int i = 0; i < 32768; i++)
{
allColor[i].r = i >> 10;
allColor[i].g = (i % 1024) >> 5;
allColor[i].b = i % 32;
allColor[i].number = 0;
}
// record times
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int nowPixel = (x + y * width) * 4;
data[nowPixel + RED] = data[nowPixel + RED] / 8;
data[nowPixel + GREEN] = data[nowPixel + GREEN] / 8;
data[nowPixel + BLUE] = data[nowPixel + BLUE] / 8;
int index = (data[nowPixel + RED] << 10) + (data[nowPixel + GREEN] << 5) + data[nowPixel + BLUE];
allColor[index].number++;
}
}
// sort
qsort(allColor, 32768, sizeof(color), compare);
// DP time
int search[32768];
memset(search, -1, sizeof(search));
// maping
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int nowPixel = (x + y * width) * 4;
int rgb[3], index = 0;
rgb[RED] = data[nowPixel + RED];
rgb[GREEN] = data[nowPixel + GREEN];
rgb[BLUE] = data[nowPixel + BLUE];
int colorKey = (rgb[RED] << 10) + (rgb[GREEN] << 5) + rgb[BLUE];
index = search[colorKey];
if (index == -1)
{
int temp = 0, distance = 999999;
// calculate distance
for (int i = 0; i < 256; i++)
{
temp = (int)(pow(rgb[RED] - allColor[i].r, 2) + pow(rgb[GREEN] - allColor[i].g, 2) + pow(rgb[BLUE] - allColor[i].b, 2));
if (temp < distance)
{
index = i;
distance = temp;
}
}
search[colorKey] = index;
}
data[nowPixel + RED] = allColor[index].r * 8;
data[nowPixel + GREEN] = allColor[index].g * 8;
data[nowPixel + BLUE] = allColor[index].b * 8;
}
}Editor is loading...