Пример #1
0
 /// <summary>
 /// Loads the image internally in the texture for image manipulation.  This is
 /// handled automatically, but it's exposed so that it can be manually controlled.
 /// </summary>
 public void CreateImage()
 {
     if (image == null)
     {
         image = texture.CopyToImage();
     }
 }
Пример #2
0
        /// <summary>
        /// Get the Color from a specific pixel on the texture.
        /// Warning: This is slow!
        /// </summary>
        /// <param name="x">The x coordinate of the pixel to get.</param>
        /// <param name="y">The y coordinate of the pixel to get.</param>
        /// <returns>The Color of the pixel.</returns>
        public Color GetPixel(int x, int y)
        {
            if (x < 0)
            {
                throw new ArgumentException("X must be greater than 0.");
            }
            if (y < 0)
            {
                throw new ArgumentException("Y must be greater than 0.");
            }

            CreateImage();

            return(new Color(texture.CopyToImage().GetPixel((uint)x, (uint)y)));
        }
Пример #3
0
 /// <summary>
 /// Loads the image internally in the texture for image manipulation.  This is
 /// handled automatically, but it's exposed so that it can be manually controlled.
 /// </summary>
 /// <param name="forceNewImage">If set to true a new image will always be created instead of only when there is no image.</param>
 public void CreateImage(bool forceNewImage = false)
 {
     if (image == null || forceNewImage)
     {
         image = texture.CopyToImage();
     }
 }
Пример #4
0
        /// <summary>
        /// Creates a pixel collider.
        /// </summary>
        /// <param name="source">The source image to create the collider from.</param>
        /// <param name="tags">The tags to register the collider with.</param>
        public PixelCollider(string source, params int[] tags) {
            texture = Textures.Load(source);
            collideImage = texture.CopyToImage();
            
            Width = texture.Size.X;
            Height = texture.Size.Y;

            AddTag(tags);
        }
Пример #5
0
        /// <summary>
        /// Creates a pixel collider.
        /// </summary>
        /// <param name="source">The source image to create the collider from.</param>
        /// <param name="tags">The tags to register the collider with.</param>
        public PixelCollider(string source, params int[] tags)
        {
            texture      = Textures.Load(source);
            collideImage = texture.CopyToImage();

            Width  = texture.Size.X;
            Height = texture.Size.Y;

            AddTag(tags);
        }
Пример #6
0
 internal Texture(SfmlTexture sfmlTexture)
 {
     SfmlImage   = sfmlTexture.CopyToImage();
     SfmlTexture = new SfmlTexture(SfmlImage);
 }
Пример #7
0
        public unsafe static void ToSurface(this Texture2D image, TextSurface surface, Color[] cachedColorArray, bool blockMode = false)
        {
#if SFML
            int     imageWidth  = (int)image.Size.X;
            int     imageHeight = (int)image.Size.Y;
            Color[] pixels      = new Color[imageWidth * imageHeight];
            using (var imageData = image.CopyToImage())
            {
                int pixelChanIndex = 0;
                fixed(byte *pixelChan = imageData.Pixels)
                {
                    fixed(Color *color = pixels)
                    {
                        for (int i = 0; i < pixels.Length; i++)
                        {
                            color[i].R = pixelChan[pixelChanIndex];
                            color[i].G = pixelChan[pixelChanIndex + 1];
                            color[i].B = pixelChan[pixelChanIndex + 2];
                            color[i].A = pixelChan[pixelChanIndex + 3];

                            pixelChanIndex += 4;
                        }
                    }
                }
            }
#elif MONOGAME
            int imageWidth  = image.Width;
            int imageHeight = image.Height;
            image.GetData <Color>(cachedColorArray);
#endif

            SurfaceEditor editor = new SurfaceEditor(surface);
            editor.Clear();
            global::System.Threading.Tasks.Parallel.For(0, imageHeight / surface.Font.Size.Y, (h) =>
                                                        //for (int h = 0; h < imageHeight / surface.Font.Size.Y; h++)
            {
                int startY = (h * surface.Font.Size.Y);
                //System.Threading.Tasks.Parallel.For(0, imageWidth / surface.Font.Size.X, (w) =>
                for (int w = 0; w < imageWidth / surface.Font.Size.X; w++)
                {
                    int startX = (w * surface.Font.Size.X);

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

                    for (int y = 0; y < surface.Font.Size.Y; y++)
                    {
                        for (int x = 0; x < surface.Font.Size.X; x++)
                        {
                            int cY = y + startY;
                            int cX = x + startX;

                            Color color = cachedColorArray[cY * imageWidth + cX];

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

                    byte sr = (byte)(allR / (surface.Font.Size.X * surface.Font.Size.Y));
                    byte sg = (byte)(allG / (surface.Font.Size.X * surface.Font.Size.Y));
                    byte sb = (byte)(allB / (surface.Font.Size.X * surface.Font.Size.Y));

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

                    float sbri = newColor.GetBrightness() * 255;

                    if (blockMode)
                    {
                        if (sbri > 204)
                        {
                            editor.SetGlyph(w, h, 219, newColor); //█
                        }
                        else if (sbri > 152)
                        {
                            editor.SetGlyph(w, h, 178, newColor); //▓
                        }
                        else if (sbri > 100)
                        {
                            editor.SetGlyph(w, h, 177, newColor); //▒
                        }
                        else if (sbri > 48)
                        {
                            editor.SetGlyph(w, h, 176, newColor); //░
                        }
                    }
                    else
                    {
                        if (sbri > 230)
                        {
                            editor.SetGlyph(w, h, (int)'#', newColor);
                        }
                        else if (sbri > 207)
                        {
                            editor.SetGlyph(w, h, (int)'&', newColor);
                        }
                        else if (sbri > 184)
                        {
                            editor.SetGlyph(w, h, (int)'$', newColor);
                        }
                        else if (sbri > 161)
                        {
                            editor.SetGlyph(w, h, (int)'X', newColor);
                        }
                        else if (sbri > 138)
                        {
                            editor.SetGlyph(w, h, (int)'x', newColor);
                        }
                        else if (sbri > 115)
                        {
                            editor.SetGlyph(w, h, (int)'=', newColor);
                        }
                        else if (sbri > 92)
                        {
                            editor.SetGlyph(w, h, (int)'+', newColor);
                        }
                        else if (sbri > 69)
                        {
                            editor.SetGlyph(w, h, (int)';', newColor);
                        }
                        else if (sbri > 46)
                        {
                            editor.SetGlyph(w, h, (int)':', newColor);
                        }
                        else if (sbri > 23)
                        {
                            editor.SetGlyph(w, h, (int)'.', newColor);
                        }
                    }
                }
            }
                                                        );
        }