private void SampleImageColorGenerator(Bitmap bitmap, Bitmap backgroundImage, int y, int uniqueColorsCount, int[] colors)
{
Color?[] bitmapColors = new Color?[uniqueColorsCount];
int nextXWithColor = -1;
for (int x = 0; x < colors.Length; x++)
{
if (bitmapColors[colors[x]] == null)
{
if (nextXWithColor < x)
{
for (int n = 1; n <= SubpixelsCount * 5; n++)
if (x + n < XResolutionInternal && bitmapColors[colors[x + n]] != null)
{
nextXWithColor = x + n;
break;
}
}
Color currentColor;
if (y % backgroundImage.Height < 5 && y / backgroundImage.Height != 0)
{
Color c1 = backgroundImage.GetPixel((x / SubpixelsCount) % backgroundImage.Width, backgroundImage.Height - 1);
Color c2 = backgroundImage.GetPixel((x / SubpixelsCount) % backgroundImage.Width, y % backgroundImage.Height);
double q = (y % backgroundImage.Height + 1) / 6.0;
currentColor = Color.FromArgb((int)Math.Round(c1.R * (1 - q) + c2.R * q), (int)Math.Round(c1.G * (1 - q) + c2.G * q), (int)Math.Round(c1.B * (1 - q) + c2.B * q));
}
else
currentColor = backgroundImage.GetPixel((x / SubpixelsCount) % backgroundImage.Width, y % backgroundImage.Height);
if (nextXWithColor > x)
{
Color nextColor = bitmapColors[colors[nextXWithColor]].Value;
double q = (double)(nextXWithColor - x) / (SubpixelsCount * 5 + 1);
bitmapColors[colors[x]] = Color.FromArgb((int)Math.Round(currentColor.R * q + nextColor.R * (1 - q)), (int)Math.Round(currentColor.G * q + nextColor.G * (1 - q)),
(int)Math.Round(currentColor.B * q + nextColor.B * (1 - q)));
}
else
bitmapColors[colors[x]] = currentColor;
}
if (x % SubpixelsCount == SubpixelsCount - 1)
{
int r = 0, g = 0, b = 0;
for (int n = 0; n < SubpixelsCount; n++)
{
r += bitmapColors[colors[x - x % SubpixelsCount + n]].Value.R;
g += bitmapColors[colors[x - x % SubpixelsCount + n]].Value.G;
b += bitmapColors[colors[x - x % SubpixelsCount + n]].Value.B;
}
Color color = Color.FromArgb((int)Math.Round((double)r / SubpixelsCount), (int)Math.Round((double)g / SubpixelsCount), (int)Math.Round((double)b / SubpixelsCount));
bitmap.SetPixel(x / SubpixelsCount, y, color);
}
}
}