public ICellSurface ToSurface(TextureConvertMode mode, int surfaceWidth, int surfaceHeight, TextureConvertBackgroundStyle backgroundStyle = TextureConvertBackgroundStyle.Pixel, TextureConvertForegroundStyle foregroundStyle = TextureConvertForegroundStyle.Block, Color[] cachedColorArray = null, ICellSurface cachedSurface = null) => throw new NotImplementedException();
示例#2
0
        /// <inheritdoc />
        public ICellSurface ToSurface(TextureConvertMode mode, int surfaceWidth, int surfaceHeight, TextureConvertBackgroundStyle backgroundStyle = TextureConvertBackgroundStyle.Pixel, TextureConvertForegroundStyle foregroundStyle = TextureConvertForegroundStyle.Block, Color[] cachedColorArray = null, ICellSurface cachedSurface = null)
        {
            if (surfaceWidth <= 0 || surfaceHeight <= 0 || surfaceWidth > _texture.Size.X || surfaceHeight > _texture.Size.Y)
            {
                throw new ArgumentOutOfRangeException("The size of the surface must be equal to or smaller than the texture.");
            }

            ICellSurface surface = cachedSurface ?? new CellSurface(surfaceWidth, surfaceHeight);

            // Background mode with simple resizing.
            if (mode == TextureConvertMode.Background && backgroundStyle == TextureConvertBackgroundStyle.Pixel)
            {
                using var resizer = GetResizedTexture(surface.Width, surface.Height);

                var colors = new Color[(int)resizer.Size.X * (int)resizer.Size.Y];
                using Image image = resizer.Texture.CopyToImage();
                byte[] imagePixels = image.Pixels;

                int colorIndex = 0;
                for (int i = 0; i < imagePixels.Length; i += 4)
                {
                    surface[colorIndex++].Background = new Color(imagePixels[i], imagePixels[i + 1], imagePixels[i + 2], imagePixels[i + 3]);
                }

                return(surface);
            }

            // Calculating color based on surrounding pixels
            Color[] pixels = GetPixels();

            int fontSizeX = (int)_texture.Size.X / surfaceWidth;
            int fontSizeY = (int)_texture.Size.Y / surfaceHeight;

            global::System.Threading.Tasks.Parallel.For(0, (int)_texture.Size.Y / fontSizeY, (h) =>
                                                        //for (int h = 0; h < imageHeight / fontSizeY; h++)
            {
                int startY = h * fontSizeY;
                //System.Threading.Tasks.Parallel.For(0, imageWidth / fontSizeX, (w) =>
                for (int w = 0; w < _texture.Size.X / fontSizeX; w++)
                {
                    int startX = w * fontSizeX;

                    float allR = 0;
                    float allG = 0;
                    float allB = 0;

                    for (int y = 0; y < fontSizeY; y++)
                    {
                        for (int x = 0; x < fontSizeX; x++)
                        {
                            int cY = y + startY;
                            int cX = x + startX;

                            Color color = pixels[cY * _texture.Size.X + cX];

                            allR += color.R;
                            allG += color.G;
                            allB += color.B;
                        }
                    }

                    byte sr = (byte)(allR / (fontSizeX * fontSizeY));
                    byte sg = (byte)(allG / (fontSizeX * fontSizeY));
                    byte sb = (byte)(allB / (fontSizeX * fontSizeY));

                    var newColor = new SadRogue.Primitives.Color(sr, sg, sb);

                    if (mode == TextureConvertMode.Background)
                    {
                        surface.SetBackground(w, h, newColor);
                    }

                    else if (foregroundStyle == TextureConvertForegroundStyle.Block)
                    {
                        float sbri = newColor.GetBrightness() * 255;

                        if (sbri > 204)
                        {
                            surface.SetGlyph(w, h, 219, newColor); //█
                        }
                        else if (sbri > 152)
                        {
                            surface.SetGlyph(w, h, 178, newColor); //▓
                        }
                        else if (sbri > 100)
                        {
                            surface.SetGlyph(w, h, 177, newColor); //▒
                        }
                        else if (sbri > 48)
                        {
                            surface.SetGlyph(w, h, 176, newColor); //░
                        }
                    }
                    else //else if (foregroundStyle == TextureConvertForegroundStyle.AsciiSymbol)
                    {
                        float sbri = newColor.GetBrightness() * 255;

                        if (sbri > 230)
                        {
                            surface.SetGlyph(w, h, '#', newColor);
                        }
                        else if (sbri > 207)
                        {
                            surface.SetGlyph(w, h, '&', newColor);
                        }
                        else if (sbri > 184)
                        {
                            surface.SetGlyph(w, h, '$', newColor);
                        }
                        else if (sbri > 161)
                        {
                            surface.SetGlyph(w, h, 'X', newColor);
                        }
                        else if (sbri > 138)
                        {
                            surface.SetGlyph(w, h, 'x', newColor);
                        }
                        else if (sbri > 115)
                        {
                            surface.SetGlyph(w, h, '=', newColor);
                        }
                        else if (sbri > 92)
                        {
                            surface.SetGlyph(w, h, '+', newColor);
                        }
                        else if (sbri > 69)
                        {
                            surface.SetGlyph(w, h, ';', newColor);
                        }
                        else if (sbri > 46)
                        {
                            surface.SetGlyph(w, h, ':', newColor);
                        }
                        else if (sbri > 23)
                        {
                            surface.SetGlyph(w, h, '.', newColor);
                        }
                    }
                }
            }
                                                        );

            return(surface);
        }