开发者

RGB Parade in WPF

开发者 https://www.devze.com 2023-01-26 05:11 出处:网络
I\'m supposed to create this RBG parade project in WPF, but I have no idea where to start. It should handl开发者_Python百科e images in 720p settings (1280x720). Any idea on where to start, I\'m comple

I'm supposed to create this RBG parade project in WPF, but I have no idea where to start. It should handl开发者_Python百科e images in 720p settings (1280x720). Any idea on where to start, I'm completely clueless. I don't want the whole project just a few pointers.

Thank you.


Ok, this is what I came up with, I'm sure there's a better solution but it works:

public ObservableCollection<double> RPercentPerColumn { get; set; }
public ObservableCollection<double> GPercentPerColumn { get; set; }
public ObservableCollection<double> BPercentPerColumn { get; set; }

private int[] GetImagePixels(ref int imageHeight, ref int imageWidth)
{
    BitmapImage image = new BitmapImage(new Uri(this.CurrentImageFile, UriKind.Absolute));

    WriteableBitmap bmp = new WriteableBitmap(image);

    int rows = bmp.PixelHeight;
    int columns = bmp.PixelWidth;

    int[] pixels = new int[bmp.PixelWidth * bmp.PixelHeight];

    bmp.CopyPixels(pixels, columns * 4, 0);

    imageHeight = bmp.PixelHeight;
    imageWidth = bmp.PixelWidth;

    return pixels;
}

private void ClearAllData()
{
    this.RPercentPerColumn.Clear();
    this.GPercentPerColumn.Clear();
    this.BPercentPerColumn.Clear();
}

public void ProcessRGBParade()
{
    if (string.IsNullOrEmpty(this.CurrentImageFile))
        return;

    ClearAllData();

    int columns = 0;
    int rows = 0;

    int[] pixels = GetImagePixels(ref rows, ref columns);

    int column = 0;
    int row = 0;

    int redColumnTotal = 0;
    int greenColumnTotal = 0;
    int blueColumnTotal = 0;
    int currentPixel = 0;

    double totalColorInColumn = 0;

    double redIntensity = 0;
    double greenIntensity = 0;
    double blueIntensity = 0;

    int r = 0;
    int g = 0;
    int b = 0;

    // logic to calculate intensity
    for (int i = 0; i < pixels.Length; i ++)
    {
        row++;

        r = (pixels[currentPixel] & 0x00FF0000) >> 16;
        g = (pixels[currentPixel] & 0x0000FF00) >> 8;
        b = (pixels[currentPixel] & 0x000000FF);

        redColumnTotal += r;
        greenColumnTotal += g;
        blueColumnTotal += b;

        totalColorInColumn += r + g + b;

        if (row == rows)
        {
            row = 0;
            column++;

            currentPixel = column;

            redIntensity = (redColumnTotal / totalColorInColumn) * 100;
            greenIntensity = (greenColumnTotal / totalColorInColumn) * 100;
            blueIntensity = (blueColumnTotal / totalColorInColumn) * 100;

            RPercentPerColumn.Add(double.IsNaN(redIntensity) ? 0 : redIntensity);
            GPercentPerColumn.Add(double.IsNaN(greenIntensity) ? 0 : greenIntensity);
            BPercentPerColumn.Add(double.IsNaN(blueIntensity) ? 0 : blueIntensity);

            redColumnTotal = 0;
            greenColumnTotal = 0;
            blueColumnTotal = 0;

            totalColorInColumn = 0;
        }
        else
        {
            currentPixel += columns;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消