/// <summary>
        /// Converts the specified BitmapPixels to grayscale.
        /// </summary>
        /// <param name="pixels">The BitmapPixels to convert.</param>
        public static void MakeGrayscal(BitmapPixels pixels)
        {
            unsafe
            {
                RGBAColor *color_ps = pixels.Pointer;

                byte new_pixel;
                for (int pixel = 0; pixel < pixels.Height * pixels.Width; pixel++, color_ps++)
                {
                    new_pixel = (byte)(color_ps->R * 0.2126 + color_ps->G * 0.7152 + color_ps->B * 0.0722);
                    *color_ps = new RGBAColor(new_pixel, new_pixel, new_pixel, color_ps->A); //set the new pixel using the existing alpha component
                }
            }
        }
        private async void FileOpenMenuItem_Click(object sender, EventArgs e)
        {
            if (OpenImageDialog.ShowDialog() == DialogResult.OK)
            {
                using (var originalBitmap = Bitmap.FromFile(OpenImageDialog.FileName) as Bitmap)
                {
                    OriginalImage.Image = originalBitmap.Clone() as Bitmap;

                    Stopwatch timer = new Stopwatch();

                    BitmapPixelsGrayscaleImage.Image = await Task.Run(() =>
                    {
                        timer.Start();

                        Bitmap grayscaleBitmap = originalBitmap.Clone() as Bitmap;

                        using (BitmapPixels grayscaleBitmapPixels = new BitmapPixels(grayscaleBitmap))
                        {
                            grayscaleBitmapPixels.Lock();

                            MakeGrayscal(grayscaleBitmapPixels);

                            timer.Stop();

                            return(grayscaleBitmap);
                        }
                    });

                    BitmapPixelsConvertTimeLabel.Text = $"{timer.ElapsedMilliseconds} milliseconds";

                    timer.Reset();

                    SetPixelsGrayscaleImage.Image = await Task.Run(() =>
                    {
                        timer.Start();

                        Bitmap grayscaleBitmap = originalBitmap.Clone() as Bitmap;

                        MakeGrayscal(grayscaleBitmap);

                        timer.Stop();

                        return(grayscaleBitmap);
                    });

                    SetPixelConvertTimeLabel.Text = $"{timer.ElapsedMilliseconds} milliseconds";
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// 読み込み済み画素値から2点を結ぶ線上の画素値を返します
        /// </summary>
        /// <param name="point1XRatio">開始点Xの割合(0~1)</param>
        /// <param name="point1YRatio">開始点Yの割合(0~1)</param>
        /// <param name="point2XRatio">終了点Xの割合(0~1)</param>
        /// <param name="point2YRatio">終了点Yの割合(0~1)</param>
        /// <returns>RGBの画素値配列(8bit)</returns>
        public ReadOnlySpan <(byte R, byte G, byte B)> GetRgbLineLevels(
            double point1XRatio, double point1YRatio, double point2XRatio, double point2YRatio)
        {
            using (var bitmapPixels = new BitmapPixels(BitmapSource))
            {
                try
                {
                    int limit(int v, int max) => (v <= 0) ? 0 : ((v >= max) ? max : v);

                    int widthMax  = bitmapPixels.Width - 1;
                    int heightMax = bitmapPixels.Height - 1;
                    int p1x       = limit((int)(point1XRatio * widthMax), widthMax);
                    int p1y       = limit((int)(point1YRatio * heightMax), heightMax);
                    int p2x       = limit((int)(point2XRatio * widthMax), widthMax);
                    int p2y       = limit((int)(point2YRatio * heightMax), heightMax);

                    int    diffX    = p2x - p1x;
                    int    diffY    = p2y - p1y;
                    double distance = Math.Sqrt(diffX * diffX + diffY * diffY);

                    var rgbs = new (byte R, byte G, byte B)[(int)distance];