AutoStereogramDemo.AutoStereogramBuilder.DefaultColorGenerator C# (CSharp) Метод

DefaultColorGenerator() приватный Метод

private DefaultColorGenerator ( Bitmap bitmap, int y, int uniqueColorsCount, int colors ) : void
bitmap System.Drawing.Bitmap
y int
uniqueColorsCount int
colors int
Результат void
        private void DefaultColorGenerator(Bitmap bitmap, int y, int uniqueColorsCount, int[] colors)
        {
            int?[] bitmapColors = new int?[uniqueColorsCount];

            for (int x = 0; x < XResolutionInternal; x++)
            {
                if (bitmapColors[colors[x]] == null)
                {
                    bool colorSet = false;

                    if (x > 0 && x < XResolutionInternal - 1)
                    {
                        int n;
                        for (n = 1; n <= SubpixelsCount && x + n < XResolutionInternal; n++)
                            if (bitmapColors[colors[x + n]] != null)
                            {
                                colorSet = true;
                                break;
                            }

                        if (colorSet)
                        {
                            int color1 = bitmapColors[colors[x - 1]].Value, color2 = bitmapColors[colors[x + n]].Value;

                            for (int m = 0; m < n; m++)
                                bitmapColors[colors[x + m]] = (int)Math.Round((double)(color1 * (n - m) + color2 * (m + 1)) / (n + 1));
                        }
                    }

                    if (!colorSet)
                    {
                        int randMax = 115 + SubpixelsCount * 35;
                        int c = ((x / SubpixelsCount > 0 ? bitmap.GetPixel(x / SubpixelsCount - 1, y).R : 128) +
                         (y > 0 ? bitmap.GetPixel(x / SubpixelsCount, y - 1).R : 128)) / 2 + random.Next(randMax) - randMax / 2 + 1;
                        bitmapColors[colors[x]] = Math.Min(Math.Max(c, 0), 255);
                    }
                }

                if (x % SubpixelsCount == SubpixelsCount - 1)
                {
                    int cAvg = 0;
                    for (int n = 0; n < SubpixelsCount; n++)
                        cAvg += bitmapColors[colors[x - n]].Value;
                    cAvg = (int)Math.Round((double)cAvg / SubpixelsCount);

                    cAvg = Math.Max(Math.Min((int)((cAvg - 128) * (0.95 + SubpixelsCount * 0.05) + 128), 255), 0);

                    bitmap.SetPixel(x / SubpixelsCount, y, Color.FromArgb(cAvg, cAvg, cAvg));
                }
            }
        }