Ejemplo n.º 1
0
        static Rgba32 GetColorFromHexString(string hexString)
        {
            Rgba32 outColor = new Rgba32();

            SixLabors.ImageSharp.PixelFormats.RgbaVector.FromHex(hexString).ToRgba32(ref outColor);
            return(outColor);
        }
Ejemplo n.º 2
0
        protected virtual Bitmap FillBitmap(bool[,] sprite, Color background, Color foreground)
        {
            int side      = sprite.GetLength(0);
            int imageSide = side * Options.Scale + Options.Border * 2;

            Bitmap img = new Bitmap(imageSide, imageSide);

            img.Mutate(context =>
            {
                context.Fill(background);
                for (int y = 0; y < side; ++y)
                {
                    for (int x = 0; x < side; ++x)
                    {
                        if (sprite[y, x])
                        {
                            context.Fill(foreground, new RectangleF(Options.Border + x * Options.Scale, Options.Border + y * Options.Scale, Options.Scale, Options.Scale));
                        }
                    }
                }
            });
            if (imageSide != Options.Size)
            {
                img = img.Resize(Options.Size, Options.Size);
            }

            return(img);
        }
Ejemplo n.º 3
0
        public static float GetPercentDifference(Bitmap imageA, Bitmap imageB, float perPixelThreshold = 0.05f)
        {
            if (imageA.Height != imageB.Height || imageA.Width != imageB.Width)
            {
                return(100.0f);
            }

            float totalPixels             = imageA.Height * imageA.Width;
            float amountOfPixelDifference = 0.0f;

            for (int y = 0; y < imageA.Height; y++)
            {
                Span <PixelColor> aSpan = imageA.GetPixelRowSpan(y);
                Span <PixelColor> bSpan = imageB.GetPixelRowSpan(y);

                for (int x = 0; x < imageA.Width; x++)
                {
                    PixelColor aPixel = aSpan[x];
                    PixelColor bPixel = bSpan[x];

                    float pixelDifference = GetPixelDifference(ref aPixel, ref bPixel);

                    if (pixelDifference >= perPixelThreshold)
                    {
                        amountOfPixelDifference += pixelDifference;
                    }
                }
            }

            return(amountOfPixelDifference / totalPixels);
        }
Ejemplo n.º 4
0
        private static unsafe void CheckImageSharp <TPixel, TRefPixel>()
            where TPixel : unmanaged, Pixel.IConvertTo
            where TRefPixel : unmanaged, SixLabors.ImageSharp.PixelFormats.IPixel <TRefPixel>
        {
            Assert.AreEqual(sizeof(TRefPixel), sizeof(TPixel));

            Span <byte> tmp    = stackalloc byte[sizeof(TPixel)];
            var         pixVal = System.Runtime.InteropServices.MemoryMarshal.Cast <byte, TPixel>(tmp);
            var         pixRef = System.Runtime.InteropServices.MemoryMarshal.Cast <byte, TRefPixel>(tmp);

            var cr = new SixLabors.ImageSharp.PixelFormats.Rgba32(255, 127, 63, 31);

            pixRef[0].FromRgba32(cr);

            var cv = pixVal[0].To <Pixel.BGRA32>();
            var f  = PixelFormat.TryIdentifyFormat <TPixel>();

            TestContext.WriteLine($"{cv.R} {cv.G} {cv.B} {cv.A}");

            Assert.Greater(cv.R, cv.G);
            Assert.Greater(cv.G, cv.B);
            if (f.HasUnpremulAlpha)
            {
                Assert.Greater(cv.B, cv.A);
            }
        }
Ejemplo n.º 5
0
        protected virtual SvgBuilder FillSvg(bool[,] sprite, Color background, Color foreground)
        {
            int     side     = sprite.GetLength(0);
            decimal offset   = Options.Border / (decimal)Options.Scale;
            decimal fullSide = side + 2m * offset;

            SvgBuilder svg = new SvgBuilder()
                             .SetViewBox(0, 0, fullSide, fullSide)
                             .Append(new SvgRect {
                PercentageWidth  = 100,
                PercentageHeight = 100,
                Color            = background
            });

            SvgPathBuilder path = new SvgPathBuilder {
                Color = foreground
            };

            for (int y = 0; y < side; ++y)
            {
                for (int x = 0; x < side; ++x)
                {
                    if (sprite[y, x])
                    {
                        path.AppendPoint(x + offset, y + offset)
                        .AppendRelativeHorizontalLine(1)
                        .AppendRelativeVerticalLine(1)
                        .AppendRelativeHorizontalLine(-1)
                        .AppendClosePath();
                    }
                }
            }

            return(svg.Append(path));
        }
Ejemplo n.º 6
0
        public static void SinglePixelComparison()
        {
            PixelColor white       = Color.White;
            PixelColor transparent = Color.Transparent;

            Assert.Equal(1.0f, TestUtils.GetPixelDifference(ref white, ref transparent));
            Assert.Equal(0.0f, TestUtils.GetPixelDifference(ref white, ref white));
        }
Ejemplo n.º 7
0
        public static unsafe Bitmap CreateRecordingFrame(Framebuffer fbuf, float startX, float startY, float drawWidth, float drawHeight)
        {
            GraphicsDevice gd   = _controller !.GraphicsDevice;
            Texture        ftex = fbuf.ColorTargets[0].Target;

            if (_recordingStager == null || _recordingStager.Width != ftex.Width || _recordingStager.Height != ftex.Height)
            {
                VeldridGraphBuffers.DoDispose(_recordingStager);
                _recordingStager = gd.ResourceFactory.CreateTexture(new TextureDescription(ftex.Width, ftex.Height,
                                                                                           1, 1, 1, PixelFormat.B8_G8_R8_A8_UNorm, TextureUsage.Staging, TextureType.Texture2D));
            }

            _commandList !.Begin();
            _commandList.CopyTexture(ftex, _recordingStager);
            _commandList.End();
            gd.SubmitCommands(_commandList);
            gd.WaitForIdle();

            if (drawWidth == -1 || drawHeight == -1)
            {
                drawHeight = _recordingStager.Height;
                drawWidth  = _recordingStager.Width;
            }

            //draw it onto a bitmap
            Bitmap bmp = new Bitmap((int)drawWidth, (int)drawHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);



            System.Drawing.Imaging.BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, (int)drawWidth, (int)drawHeight),
                                                                  System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            byte *scan0 = (byte *)data.Scan0;

            MappedResourceView <SixLabors.ImageSharp.PixelFormats.Rgba32> res = gd.Map <SixLabors.ImageSharp.PixelFormats.Rgba32>(_recordingStager, MapMode.Read);

            for (int y = 0; y < drawHeight; y += 1)
            {
                for (int x = 0; x < drawWidth; x += 1)
                {
                    int xPixel = (int)startX + x;
                    int yPixel = (int)startY + y;
                    SixLabors.ImageSharp.PixelFormats.Rgba32 px = res[xPixel, yPixel];
                    byte *ptr = scan0 + y * data.Stride + (x * 4);
                    ptr[0] = px.R;
                    ptr[1] = px.G;
                    ptr[2] = px.B;
                    ptr[3] = 255;
                }
            }
            bmp.UnlockBits(data);
            gd.Unmap(_recordingStager);

            return(bmp);
        }
Ejemplo n.º 8
0
        public static IMAGE ToImageSharp(this Bitmap image)
        {
            var dst = new IMAGE(image.Width, image.Height);

            for (int y = 0; y < dst.Height; ++y)
            {
                for (int x = 0; x < dst.Width; ++x)
                {
                    dst[x, y] = new SixLabors.ImageSharp.PixelFormats.Rgba32(image[x, y]);
                }
            }

            return(dst);
        }
Ejemplo n.º 9
0
        static string GetClosestColorName(Rgba32 color)
        {
            double    closestSquaredDistance = SquaredColorDistance(color, table[0].color);
            ColorData closestColor           = table[0];

            foreach (ColorData data in table)
            {
                double squaredDistance = SquaredColorDistance(color, data.color);

                if (squaredDistance < closestSquaredDistance)
                {
                    closestSquaredDistance = squaredDistance;
                    closestColor           = data;
                }
            }

            return(closestColor.name);
        }
Ejemplo n.º 10
0
        static int GetYear(Bitmap image, bool isOld, bool isBlackAndWhitePhoto, int?extractedYear)
        {
            if (extractedYear.HasValue)
            {
                return(extractedYear.Value);
            }

            int maxYear = 1917;
            int minYear = 500;

            if (isOld && isBlackAndWhitePhoto)
            {
                maxYear = 1900;
                minYear = 1830;
            }
            else if (isBlackAndWhitePhoto)
            {
                // Estimate of the timeline of black and white photos
                maxYear = 1930;
                minYear = 1830;
            }
            else if (isOld)
            {
                // Make a guess that old stuff isn't modern but also isn't ancient
                maxYear = 1900;
                minYear = 1000;
            }

            PixelColor pixelSampleColor = image[image.Width / 3, image.Height / 3];

            float red   = (float)pixelSampleColor.R / 255.0f;
            float green = (float)pixelSampleColor.G / 255.0f;
            float blue  = (float)pixelSampleColor.B / 255.0f;

            float scale = (red + green + blue) / 3.0f;

            int year = (int)(minYear + (maxYear - minYear) * scale);

            return(year);
        }
Ejemplo n.º 11
0
 public static Color FromHex(uint argb) => Color.FromArgb((int)argb);
Ejemplo n.º 12
0
 public static Color FromRgb(double r, double g, double b) => Color.FromArgb((byte)r, (byte)g, (byte)b);
Ejemplo n.º 13
0
 public static Color FromRgb(int r, int g, int b) => Color.FromArgb((byte)r, (byte)g, (byte)b);
Ejemplo n.º 14
0
 public static Color FromRgb(byte r, byte g, byte b) => Color.FromArgb(r, g, b);
Ejemplo n.º 15
0
 static double SquaredColorDistance(Rgba32 color1, Rgba32 color2)
 {
     return(System.Numerics.Vector4.DistanceSquared(color1.ToVector4(), color2.ToVector4()));
 }
Ejemplo n.º 16
0
        public static float GetPixelDifference(ref PixelColor a, ref PixelColor b)
        {
            float pixelDistance = GetManhattanDistanceInRgbaSpace(ref a, ref b);

            return(pixelDistance / (255.0f * 4.0f));
        }
Ejemplo n.º 17
0
 public ColorData(Rgba32 inColor, string inName)
 {
     color = inColor;
     name  = inName;
 }
Ejemplo n.º 18
0
 private static int GetManhattanDistanceInRgbaSpace(ref PixelColor a, ref PixelColor b)
 {
     return(Diff(a.R, b.R) + Diff(a.G, b.G) + Diff(a.B, b.B) + Diff(a.A, b.A));
 }
Ejemplo n.º 19
0
 static int GetManhattanDistanceInRgbaSpace(ref ImageSharpRgba32 a, ref ImageSharpRgba32 b)
 {