Beispiel #1
0
        public void ToGrayscaleTest()
        {
            using Bitmap bmp = Icons.Information.ExtractBitmap(new Size(256, 256));

            new PerformanceTest {
                Iterations = 100, CpuAffinity = null
            }
            .AddCase(() =>
            {
                using var result = new Bitmap(bmp.Width, bmp.Height);
                using (Graphics g = Graphics.FromImage(result))
                {
                    // Grayscale color matrix
                    var colorMatrix = new ColorMatrix(new float[][]
                    {
                        new float[] { ColorExtensions.RLum, ColorExtensions.RLum, ColorExtensions.RLum, 0, 0 },
                        new float[] { ColorExtensions.GLum, ColorExtensions.GLum, ColorExtensions.GLum, 0, 0 },
                        new float[] { ColorExtensions.BLum, ColorExtensions.BLum, ColorExtensions.BLum, 0, 0 },
                        new float[] { 0, 0, 0, 1, 0 },
                        new float[] { 0, 0, 0, 0, 1 }
                    });

                    using (var attrs = new ImageAttributes())
                    {
                        attrs.SetColorMatrix(colorMatrix);
                        g.DrawImage(bmp, new Rectangle(0, 0, result.Width, result.Height), 0, 0, result.Width, result.Height, GraphicsUnit.Pixel, attrs);
                    }
                }
            }, "Graphics.DrawImage(..., ImageAttributes)")
            .AddCase(() =>
            {
                using var result = new Bitmap(bmp.Width, bmp.Height);
                using IReadableBitmapData src = bmp.GetReadableBitmapData();
                using IWritableBitmapData dst = result.GetWritableBitmapData();
                IReadableBitmapDataRow rowSrc = src.FirstRow;
                IWritableBitmapDataRow rowDst = dst.FirstRow;
                do
                {
                    for (int x = 0; x < src.Width; x++)
                    {
                        rowDst[x] = rowSrc[x].ToGray();
                    }
                } while (rowSrc.MoveNextRow() && rowDst.MoveNextRow());
            }, "Sequential processing")
            .AddCase(() =>
            {
                using var result = bmp.ToGrayscale();
            }, "ImageExtensions.ToGrayscale")
            .DoTest()
            .DumpResults(Console.Out);
        }
Beispiel #2
0
        private static Bitmap ReadRawBitmap(BinaryReader br)
        {
            var size        = new Size(br.ReadInt32(), br.ReadInt32());
            var pixelFormat = (PixelFormat)br.ReadInt32();

            Color[] palette = null;
            if (pixelFormat.ToBitsPerPixel() <= 8)
            {
                palette = new Color[br.ReadInt32()];
                for (int i = 0; i < palette.Length; i++)
                {
                    palette[i] = Color.FromArgb(br.ReadInt32());
                }
            }

            var result = new Bitmap(size.Width, size.Height, pixelFormat);

            if (palette != null)
            {
                ColorPalette resultPalette = result.Palette;
                if (resultPalette.Entries.Length != palette.Length)
                {
                    resultPalette = (ColorPalette)Reflector.CreateInstance(typeof(ColorPalette), palette.Length);
                }
                for (int i = 0; i < palette.Length; i++)
                {
                    resultPalette.Entries[i] = palette[i];
                }
                result.Palette = resultPalette;
            }

            using (IWritableBitmapData data = result.GetWritableBitmapData())
            {
                int width = data.RowSize >> 2;
                IWritableBitmapDataRow row = data.FirstRow;
                do
                {
                    for (int x = 0; x < width; x++)
                    {
                        row.WriteRaw(x, br.ReadInt32());
                    }
                } while (row.MoveNextRow());
            }

            return(result);
        }
        public void ClearTest(PixelFormat pixelFormat, uint argb)
        {
            const int size  = 512;
            Color     color = Color.FromArgb((int)argb);

            new PerformanceTest {
                TestName = $"{pixelFormat} {size}x{size}", Iterations = 10, CpuAffinity = null
            }
            .AddCase(() =>
            {
                using var bmp = new Bitmap(size, size, pixelFormat);
                using var g   = Graphics.FromImage(bmp);
                g.Clear(color);
            }, "Graphics.Clear")
            .AddCase(() =>
            {
                using var bmp = new Bitmap(size, size, pixelFormat);
                using IWritableBitmapData acc = bmp.GetWritableBitmapData();
                var c = new Color32(color);
                IWritableBitmapDataRow row = acc.FirstRow;
                do
                {
                    for (int x = 0; x < acc.Width; x++)
                    {
                        row[x] = c;
                    }
                } while (row.MoveNextRow());
            }, "Sequential clear")
            .AddCase(() =>
            {
                using var bmp = new Bitmap(size, size, pixelFormat);
                bmp.Clear(color);
            }, "BitmapDataAccessor.Clear")
            .DoTest()
            .DumpResults(Console.Out);
        }