Files
TrackTrendsDoc/temp_annexes/Code/DriverDrsWindow.md
T

3.1 KiB

DriverDrsWindow.cs

/// Author : Maxime Rohmer
/// Date : 08/05/2023
/// File : DriverDrsWindow.cs
/// Brief : Window containing DRS related method and infos
/// Version : 0.1

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tesseract;

namespace Test_Merge
{
    internal class DriverDrsWindow:Window
    {
        private static int EmptyDrsGreenValue = -1;
        private static Random rnd = new Random();
        public DriverDrsWindow(Bitmap image, Rectangle bounds,bool generateEngine = true) : base(image, bounds,generateEngine)
        {
            Name = "DRS";
        }
        public override async Task<object> DecodePng()
        {
            bool result = false;
            int greenValue = GetGreenPixels();
            if (EmptyDrsGreenValue == -1)
                EmptyDrsGreenValue = greenValue;

            if (greenValue > EmptyDrsGreenValue + EmptyDrsGreenValue / 100 * 30)
                result = true;

            return result;
        }
        private unsafe int GetGreenPixels()
        {
            int tot = 0;

            Bitmap bmp = WindowImage;
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);
            int bytesPerPixel = Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;

            unsafe
            {
                byte* ptr = (byte*)bmpData.Scan0.ToPointer();
                for (int y = 0; y < bmp.Height; y++)
                {
                    byte* currentLine = ptr + (y * bmpData.Stride);
                    for (int x = 0; x < bmp.Width; x++)
                    {
                        byte* pixel = currentLine + (x * bytesPerPixel);

                        byte blue = pixel[0];
                        byte green = pixel[1];
                        byte red = pixel[2];

                        if (green > blue * 1.5 && green > red * 1.5)
                        {
                            tot++;
                        }
                    }
                }
            }
            bmp.UnlockBits(bmpData);

            return tot;
        }
        public Rectangle GetBox()
        {
            var tessImage = Pix.LoadFromMemory(ImageToByte(WindowImage));
            Engine.SetVariable("tessedit_char_whitelist", "");
            Page page = Engine.Process(tessImage);

            using (var iter = page.GetIterator())
            {
                iter.Begin();
                do
                {
                    Rect boundingBox;

                    // Get the bounding box for the current element
                    if (iter.TryGetBoundingBox(PageIteratorLevel.Word, out boundingBox))
                    {
                        page.Dispose();
                        return new Rectangle(boundingBox.X1, boundingBox.X2, boundingBox.Width, boundingBox.Height);
                    }
                } while (iter.Next(PageIteratorLevel.Word));

                page.Dispose();
                return new Rectangle(0, 0, 0, 0);
            }
        }
    }
}